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

Amath1an
  • 23
  • 1
  • 7

2 Answers2

0

you have to use the comparison operator "==" when comparing values: otherwise you are assigning values (in this case you were assigning $case to be 1 and then the message was "co".

$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'];
gavgrif
  • 15,194
  • 2
  • 25
  • 27
0

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'];
Md. Sahadat Hossain
  • 3,210
  • 4
  • 32
  • 55
  • ha ha ha - its a race to get the same answer out @Md. Sahadat Hossain :)) – gavgrif Apr 17 '16 at 05:21
  • You have no idea how mad you just made me. I've been freaking getting so pissed off with the constant thoughts. "WHY YOU NO WORKS?!?!?!?!" Well, turns out I'm an idiot. – Amath1an Apr 17 '16 at 05:36