Monday 27 August 2012

PHP Type Casting


PHP's type casting feature is amazing.

refer bellow example.

<?php
$a="10";
$b="1E1";

if($a==$b)
{
    echo "true";
}
else
{
    echo "false";
}
?>

so what will answer ? on first look you will defiantly say answer is false, but answer is true.

so now see how it is possible, described below.

In PHP if you take string "1E1" than if first letter is 0 it will consider 0, if first letter will 1 it will consider 1.
Now E1= exponent of 10= 10*1 so answer is 10.

So formula is : 1E1 = 1*10*1 =10.

so $a=$b. condition true.

But if you type === in if condition it will return false because it will also consider the type of variable.

No comments:

Post a Comment