-3

So recently i wrote some code with a conditional if statement like the following...

if ($page['title'] = 'current_page') {
   'valid code'
}

Now what happened as a result is that this code was deemed as valid and when i ran the code it caused an overload of requests on my database and caused many unwanted issues.

Obviously the code should have been the following

if ($page['title'] == 'current_page') {
   'valid code'
}

Missing out an '=' was the cause of my problems. What i want to understand is why is deemed as valid code and what issues could this cause if left unnoticed.

Key
  • 396
  • 4
  • 15

3 Answers3

2

It is deemed as valid because it is not a syntax error. When you do this:

if ($page['title'] = 'current_page')

the = which is the assignment operator tells PHP to assign the value current_page to the array variagle $page['title'], irrespective of whatever that variable contained earlier. Effectively, the $page['title'] variable now contains the value current_page. Now, a successful assignment operation evaluates to the boolean value TRUE, which makes the entire if condition true.

What you have is a logic error. It is not caught by the interpreter/compiler. Rather such errors cause (serious) issues at runtime (as what you experienced) if left undetected.

One way some people advocate to avoid such errors is to invert the order of your operands thus:

if('current_page' = $page['title'])

In this case, PHP sees that you are trying to assing a variable to a literal, which is an invalid operation, and reports an error, which is caught at compile time. The error forces you to look clearly at your code, and to fix it by using the comparison operator ==

if('current_page' == $page['title'])

Bottom line: The = is the ASSIGNMENT operator, while the == is the COMPARISON operator. Being aware of the distinction between ASSIGNMENT and COMPARISON will help you write better code.

NaijaProgrammer
  • 2,892
  • 2
  • 24
  • 33
1

It's because you are allowed to assign a value to variable inside of any statement. This means, that something like this works:

if($row = function_that_gives_something_or_null()) {
    //do something with $row
}
else {
    //$row was equal to false
}

If you leave it unnoticed it may lead to quite hard to debug logical problems.

Luki
  • 214
  • 1
  • 7
  • As per your edit; I take it you saw [my comment](http://stackoverflow.com/questions/30304783/understand-php-conditional-statement#comment48705081_30304783) ;-) – Funk Forty Niner May 18 '15 at 13:46
  • Ah thanks, cannot believe i missed that, assign variables in functions all the time. – Key May 18 '15 at 13:51
0

Because single equals sign is just not meant to be used that way. The reason is that in your case, $page['title'] = 'current_page' sets the variable, and then always evaluates if ($page['title']) instead which will always be true except for if you siad if ($page['title'] = false)

You use = for assignments;

$number = 3

double == is loose comparison (with typecasting).

if ($string == 'test') // returns true or false

triple === is strict comparison (datatypes must match and the value must match).

if ($string === $string) // returns true if those 2 objects are identical

See here for more information.

Community
  • 1
  • 1
Arturs Vancans
  • 4,531
  • 14
  • 47
  • 76