-4

The php code is returing true in both cases show below. I dont know why?

<?php

$cid = 150;

if ($cid=100)
{
echo $cid;
echo "<BR>";
}

if ($cid==100)
{
echo "NEW";
echo "<BR>";
echo $cid;
echo "<BR>";
}

?>

The output is:
100
NEW
100


Why is the if condition not working?

2
  • 2
    Because = is not the same as ==. Commented Apr 22, 2016 at 15:52
  • Just simply compare your code line by line and you already see it. Commented Apr 22, 2016 at 15:53

1 Answer 1

2

In the first if-statement you are assigning 100 to $cid, not comparing. You're using a single = instead of ==. So in the first statement $cid is set to 100. When it comes to the second if-statement, $cid has a value of 100. So the conditional evaluates in a truthy value.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. I got it. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.