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
?>

Comments

Popular posts from this blog

Timus 1209. 1, 10, 100, 1000... accepted solution in C

Timus Problem 1086. Cryptography Accepted Solution in C

Timus 1083. Factorials!!! Accepted Solution in C