PHP Type Juggling & Type Casting Tutorial
|PHP Type Juggling
As per PHP.net – PHP does not require explicit type definition in a variable’s declaration, a variable type is determined in which context variable is used.
For example: if a string value is assigned to a variable then it became string and if an integer assigned to a variable then it becomes an integer.
EG:
$var = "Hi Gaurav"; // String $var = 7; // Integer
Have you even listen term “PHP is loosely type programming language” ?
I am 100% sure YES. And this is common interview question for PHP developers also. But many of developers always confuse why we call PHP loosely type programming language.
So here is the answer below :
When using “==” operator, PHP uses the loose comparison of type juggling. Loose comparison gives developers to compare values while the have different data types.
Now, we understood why PHP is defined as a loosely typed programming language, it means we don’t need to define the type of variable while defining. It auto-convert data into common comparable data type while comparing.
While comparing different data types, PHP always tries to convert the string data type to integer first. It checks the beginning of the string if there is any number in string can be used for comparison.
In PHP, when we perform an arithmetic operation on any expression that contains a string, that string interpreted as the appropriate numeric data type for evaluating the expressions.
So, if the string begins with one or more numeric character then remaining string data will be ignored and if the string begins with a non-numeric character, then it evaluates to zero.
For Example –
$var = "2"; /* It's value will be 2 a numeric data type only */
<?php $foo = "3"; $foo = $foo + "15 dos"; echo $foo ; ?>
The output of this will be 18 only a numeric data type, as we know in string data type only numeric value is considered or evaluates to zero.
Note – If one of operand is float then output will be float value, otherwise output will be integer value only.
<?php $foo = "0"; // $foo is string (ASCII 48) $foo += 2; // $foo is now an integer (2) $foo = $foo + 1.3; // $foo is now a float (3.3) $foo = 5 + "10 Hello world"; // $foo is integer (15) $foo = 5 + "10 hello world"; // $foo is integer (15) ?>
Above in 3rd row, $foo will be float value because one value is float.
But this type juggling can lead to serious security issues.
That’s the reason many programmers don’t like PHP because some times it’s unpredictable behaviors.
So how to avoid type juggling – Always use strict (===) for your default conversions. (===) their equal signs mean that comparisons should take the data type also in consideration.
If data type differs, PHP will not do any conversions, And it will return FALSE.
If you need to convert types, perform explicit type conversion using a cast. I explained below PHP typecasting. Always be proactive while writing a security-sensitive code.
PHP Type Casting
While dealing with PHP variable data type, if we don’t like to use PHP automatic type juggling conversion feature then we can use PHP explicit typecasting. In this, we use the desired type in parenthesis before the variable which is to be cast.
<?php $test = 10; /* $test is an integer */ $result = (boolean) $test ; /* $result is boolean */ ?>
The casts allowed are:
- (int), (integer) – cast to integer
- (bool), (boolean) – cast to boolean
- (float), (double), (real) – cast to float
- (string) – cast to string
- (array) – cast to array
- (object) – cast to object
- (unset) – cast to NULL (PHP 5)
<?php $foo = 50; // $foo is an integer $str = "$foo"; // $str is a string $fst = (string) $foo; // $fst is also a string // This prints out that "they are the same" if ($fst === $str) { echo "they are the same"; } >?php
Hope this post help you guys to understand PHP type juggling and typecasting. If I missed something, please let me know in the comments section.
Thanks for reading.