Home Tutorial Php Phpbasics Tutorial PHP Type Juggling

 
 

PHP Type Juggling
Posted on: March 16, 2010 at 12:00 AM
Advertisement
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/>";

$temp=true;

$temp+=23;

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

$temp=true;

$temp+="string";

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

unset($temp);

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

?>

 

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

Example:

<?php

$temp=12;

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/>";

$temp=12;

$temp=(boolean)$temp;

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

$temp=12;

$temp=(string)$temp;

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

$temp=12;

$temp=(array)$temp;

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

$temp=12;

$temp=(unset)$temp;

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=(int)$temp;

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

$temp=12.12;

$temp=(boolean)$temp;

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

$temp=12.12;

$temp=(string)$temp;

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

$temp=12.12;

$temp=(array)$temp;

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

$temp=12.12;

$temp=(unset)$temp;

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=(float)$temp;

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

$temp="String";

$temp=(boolean)$temp;

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

$temp="String";

$temp=(int)$temp;

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

$temp="String";

$temp=(array)$temp;

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

$temp="String";

$temp=(unset)$temp;

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=(float)$temp;

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

$temp=true;

$temp=(boolean)$temp;

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

$temp=true;

$temp=(int)$temp;

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

$temp=true;

$temp=(array)$temp;

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

$temp=true;

$temp=(unset)$temp;

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

?>

 

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

Advertisement

Related Tags for PHP Type Juggling:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 16, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com