Reputation: 308
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?
Upvotes: 9
Views: 2023
Reputation: 2488
Certain functions return "mixed" datatypes, i.e. might return boolean "false" for failure and integer "0" as a valid return value.
When checking return values, it is sometimes neccessary to use === comparsion in order 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"
Upvotes: 0
Reputation: 437336
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.
Upvotes: 4
Reputation: 1465
By definition, it does not work the same between each type. It is advisable to keep the same type during the life cycle of the program to prevent incorrect values.
$val = false // (int) $val => 0
$val = 0.0010 // (int) $val => 0
Upvotes: 2
Reputation: 28722
You shouldn't worry about the types, php will make them as needed, when you have to use the types when calculating, validate them using three === (type specific comparison) that they are still valid as that type of cast them to the desired type when needed
1 === 1 = true
1 === "1" = false
1 === true = false
true === true = true
false === false = true
"hello" === "hello" = true
etc...
type casting
$numberofpizzas = $_POST['number'];
$price = 16;
$totalprice = $price * (int)$numberofpizzas
Upvotes: 0