Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type casting in PHP - security and efficiency

Tags:

php

casting

I understand that PHP is a weakly-typed language. My question is: on balance, is it desirable to initialise variables in PHP as specific types, having regard to security and efficiency concerns? Or would this be swimming needlessly against the tide?

Examples of specific initialisations:

$name = (string) "PHP"; // string
$pizzaToppings = array("tomato", "cheese", "pepperoni"); // array
$angle = (integer) 60; // integer

The following article has helped, but not answered, my question: PHP Typecasting - Good or bad?

like image 544
Grant_Bailey Avatar asked Jul 03 '13 10:07

Grant_Bailey


People also ask

What is PHP type casting and how to use it?

In this tutorial, we will learn about PHP Type Casting concepts, which support converting variables from one data type into another data type. In PHP, we don’t have to specify the variable’s data type while declaring it; PHP automatically defines the data type based on the value of that variable.

Can you cast a user defined class in PHP?

This approach can only be used if initial class contains only variables as members. Note: In general, PHP doesn’t allow type casting of user defined classes, while conversion/casting can be achieved indirectly by approaches presented above. How to merge two PHP objects?

How do you cast a variable to another variable in PHP?

You can cast one type of values into another by adding your desired type in parentheses before the variable that you want to cast. There are six main type casts allowed in PHP. These are listed below: Two additional type casts are binary casting and null casting.

What is array type casting method in JavaScript?

The Array type casting method is used to convert a single value from integer, float, string, boolean data types into an array. The first element’s index always will be 0, and the total count of an array is 1. Please find below the examples,


1 Answers

PHP always needs to know the "current type" of a value before it can use it for any purpose, including initializing a variable. This "current type" is metadata (an enumeration) that goes together with all values.

In your example code the casts are meaningless because you are initializing variables using literal values, which are always of the obvious type:

$s = "foo";
echo is_string($s); // 1

$s = (string)"foo";
echo is_string($s); // also 1

The same goes for the integer and array.

There is at least one case where the type of the variable would be something other than you might expect at first sight:

$i = PHP_INT_MAX + 1; // or use something like 999999999999
echo gettype($i); // "double"!

In this case using a cast would make $i an integer, but it would also change its value:

$i = (int)(PHP_INT_MAX + 1);
echo gettype($i); // "integer"
echo $i; // a very large negative number -- what?!?

Of course this is not caused by a missing cast, but is rather an artifact of how numbers are treated in PHP. So the conclusion is clear: there is no point in using casts when initializing with literals.

If you are initializing a variable that you intend to use as type X with a value that is of a different type Y (or of an unknown type) then there is a reason to use an explicit cast: documenting in code how the variable is going to be used going forward. But don't overestimate the benefit: this information is only for human consumption; PHP will automatically do the usual type conversions whenever you try to use a variable as a different type than it is.

like image 167
Jon Avatar answered Nov 15 '22 15:11

Jon