How '0' becomes a false value in PHP
<?php
if('0')
{
echo 1;
}
else
{
echo 0;
}
// outputs 0, because 'if' statement needs to convert '0' into boolean type value to work and in PHP (bool)'0' creates boolean false
if('0' == false) // because of loose comparison '0' will be converted into boolean type automatically
{
echo 1;
}
else
{
echo 0;
}
//ouputs 1
if('0' === false)
{
echo 1;
}
else
{
echo 0;
}
// outputs 0, because '0' is a string after all and false is boolean, so their type didn't match here and for strict comparison no type conversion happened
?>
if('0')
{
echo 1;
}
else
{
echo 0;
}
// outputs 0, because 'if' statement needs to convert '0' into boolean type value to work and in PHP (bool)'0' creates boolean false
if('0' == false) // because of loose comparison '0' will be converted into boolean type automatically
{
echo 1;
}
else
{
echo 0;
}
//ouputs 1
if('0' === false)
{
echo 1;
}
else
{
echo 0;
}
// outputs 0, because '0' is a string after all and false is boolean, so their type didn't match here and for strict comparison no type conversion happened
?>
Comments
Post a Comment