Type of casting in PHP - safety and efficiency

I understand that PHP is a weakly typed language. My question is: on balance, is it desirable to initialize variables in PHP as concrete types, given security and efficiency issues? Or will it be useless to swim against the tide?

Examples of specific initializations:

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

The following article helped, but did not answer, my question: Is PHP Typecasting good or bad?

+6
source share
4 answers

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

In your sample code, castings are pointless because you initialize variables using literal values ​​that are always of an obvious type:

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

The same goes for integers and array.

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

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

In this case, using cast will make $i an integer, but it will 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 application, but rather an artifact of how numbers are processed in PHP. So, the conclusion is clear: it makes no sense to use casts when initializing with literals .

If you initialize a variable that you intend to use as type X with a value having a different type of Y (or an unknown type), then there is reason to use an explicit cast: documenting in code how the variable will be used in the future. But do not overestimate the benefits: this information is intended for human consumption only; PHP will automatically perform normal type conversions when you try to use a variable as a different type than it is.

+4
source

By definition, it does not work the same for each type. It is advisable to keep the same type throughout the program life cycle to prevent incorrect values.

  $val = false // (int) $val => 0 $val = 0.0010 // (int) $val => 0 

All types of testing methods

+2
source

You don’t have to worry about types, php will make them as needed, when you have to use types when calculating, check them using three === (specific comparison type), which they are still valid as this type, if necessary, transfer them to the right one a type

 1 === 1 = true 1 === "1" = false 1 === true = false true === true = true false === false = true "hello" === "hello" = true etc... 

casting

 $numberofpizzas = $_POST['number']; $price = 16; $totalprice = $price * (int)$numberofpizzas 
0
source

Some functions return "mixed" data types, i.e. can return a boolean "false" for failure and an integer "0" as a valid return value.

When checking return values, it is sometimes necessary to use === comparsion to get the correct results.

Take this example:

 $s = 'Test'; $c = strpos($s, 'T'); // The letter 'T' is at the first position, at index 0 if (false == $c) ... // evaluates to "true" because of the weak type comparsion of "0" and "false" if (false === $c) ... // evaluates to "false" 
0
source

Source: https://habr.com/ru/post/948614/


All Articles