PHP Type Juggling


 

PHP Type Juggling

This tutorial covers up many types of implicit and explicit type casting in PHP. Examples will illustrate the concept of PHP type juggling

This tutorial covers up many types of implicit and explicit type casting in PHP. Examples will illustrate the concept of PHP type juggling

Type Juggling In PHP:

Unlike other programming languages like C,C++ and Java, PHP does not require to declare any variable specifically. It means if we assign a float value to a variable then it becomes float type and if we assign a string value then the variable will become string datatype.

Another example of implicit typecasting is if we add two integer variables then the result is integer type, but if we add one integer and one float type variable then the result will of float data type because float data type has a higher precedence than integer.

PHP Juggling Type Example:

<?php

$temp="0";

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp="12";

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp=12;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp+=12;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp+=12.12;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp=12;

$temp+="string";

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp="12.12";

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp=12.12;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp+=23;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp+="string";

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp=true;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp+=12.12;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

0

$temp=true;

$temp+=23;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

1

$temp=true;

$temp+="string";

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

2

unset($temp);

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

?>

3

 

Output:

Value of temp is:0 and data type is: string
Value of temp is:12 and data type is: string
Value of temp is:12 and data type is: integer
Value of temp is:24 and data type is: integer
Value of temp is:36.12 and data type is: double
Value of temp is:12 and data type is: integer
Value of temp is:12.12 and data type is: string
Value of temp is:12.12 and data type is: double
Value of temp is:35.12 and data type is: double
Value of temp is:35.12 and data type is: double
Value of temp is:1 and data type is: boolean
Value of temp is:13.12 and data type is: double
Value of temp is:24 and data type is: integer
Value of temp is:1 and data type is: integer
Value of temp is: and data type is: NULL

4

Example:

<?php

$temp=12;

5

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp=(float)$temp;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

6

$temp=12;

$temp=(boolean)$temp;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

7

$temp=12;

$temp=(string)$temp;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

8

$temp=12;

$temp=(array)$temp;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

9

$temp=12;

$temp=(unset)$temp;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

0

$temp=12.12;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp=(int)$temp;

1

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp=12.12;

$temp=(boolean)$temp;

2

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp=12.12;

$temp=(string)$temp;

3

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp=12.12;

$temp=(array)$temp;

4

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp=12.12;

$temp=(unset)$temp;

5

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp="String";

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

6

$temp=(float)$temp;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp="String";

7

$temp=(boolean)$temp;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp="String";

8

$temp=(int)$temp;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp="String";

9

$temp=(array)$temp;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp="String";

0

$temp=(unset)$temp;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>"

$temp=true;

1

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

$temp=(float)$temp;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

2

$temp=true;

$temp=(boolean)$temp;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

3

$temp=true;

$temp=(int)$temp;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

4

$temp=true;

$temp=(array)$temp;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>";

5

$temp=true;

$temp=(unset)$temp;

echo "Value of temp is:".$temp." and data type is: ".gettype($temp)."<br/>"; 

6

?>

 

7

Output:

Value of temp is:12 and data type is: integer
Value of temp is:12 and data type is: double
Value of temp is:1 and data type is: boolean
Value of temp is:12 and data type is: string
Value of temp is:Array and data type is: array
Value of temp is: and data type is: NULL
Value of temp is:12.12 and data type is: double
Value of temp is:12 and data type is: integer
Value of temp is:1 and data type is: boolean
Value of temp is:12.12 and data type is: string
Value of temp is:Array and data type is: array
Value of temp is: and data type is: NULL
Value of temp is:String and data type is: string
Value of temp is:0 and data type is: double
Value of temp is:1 and data type is: boolean
Value of temp is:0 and data type is: integer
Value of temp is:Array and data type is: array
Value of temp is: and data type is: NULL
Value of temp is:1 and data type is: boolean
Value of temp is:1 and data type is: double
Value of temp is:1 and data type is: boolean
Value of temp is:0 and data type is: integer
Value of temp is:Array and data type is: array
Value of temp is: and data type is: NULL

Ads