Amath1an Amath1an - 7 months ago 10
PHP Question

PHP - $x=0 -- Why is it passing off as being equal to 1?

I have a few lines of code

$case=0;
file_put_contents("text.txt", $case, FILE_APPEND);
if ($case = 1)
{
$message['a']="co";
}
if ($case = 0)
{
$message['a']="to";
}
echo $message['a'];


It will echo "co". Why is this? The file_put contents puts "0". However the if statement thinks it is 1 for some reason...

Answer

You are doing wrong in if condition. You doing assign instead of comparison.
So here is the solution.

$case=0;
file_put_contents("text.txt", $case, FILE_APPEND);
if ($case == 1)
{
    $message['a']="co"; 
}
if ($case == 0)
{
    $message['a']="to";
}
echo $message['a'];