Tutorials PHP Comparisons and Order of Operations

Comparisons and Order of Operations

Today's tutorial will be about how PHP compares things and in which order. IF statements are simple to understand but difficult to master. It's used to check the validity of a statement. If a statement is true, then do something.

Comparison Operators

So what can you check with the IF statement?

<for integers. Checks if the number on the left is smaller than the number on the right
<=for integers. Checks if the number on the left is smaller than or equal to the number on the right
>for integers. Checks if the number on the left is greater than the number on the right
>=for integers. Checks if the number on the left is greater than or equal to the number on the right
==checks if the variable on the left is equal to the variable on the right
!=checks if the variable on the left is not equal to the variable on the right
&&, ANDchecks if both statements are true. 'AND' is the same as '&&' but lower in rank.
||, ORchecks if either statement is true. 'OR' is the same as '||' but lower in rank.

Order of operations

For those of you who didn't drop out of elementary school math, you should remember the order of operations. BEDMAS or brackets, exponents, division, multiplication, addition, and subtraction. And I'm sure you remember the first time you plugged in something like 1+2x3, it would produce a different answer depending on your calculator. If you used a cheap dollar store calculator, it would go 1+2=3, 3x3=9; thus the final answer would be 9. If you had a scientific calculator, it would go 2x3=6 first, and then 1+6=7; thus the final answer would be 7. PHP is like a scientific calculator with its own order of operations.

This is the order in which PHP works in. The ones on the same line are equally important and get read from left to right, just like division with multiplication and addition with subtraction are equally important. And like math, if you need to manipulate the order and do subtraction before multiplication, you have to put it in brackets. ie. 2x(3+1) will return 8 on a scientific calculator.

This is PHP's order of operations, from top to bottom.

()<, <=, >, >===, !=&&||ANDOR

Note: This is not the full list of operations. This is my shortened version that will satisfy 90% of what you will probably need to compare. For the full list, please read php.net.

Why should I care?

Because the order can determin what the final outcome is. Like in math, forgetting the order will give you a totally different answer. Consider these two IF statements that, on a simple glance, may look the same:

$x = 'red';$y = 'blue';$z = 'green';if($x=='yellow' and $y=='purple' or $z=='green') {  echo 'correct';}else {  echo 'incorrect';}if($x=='yellow' and $y=='purple' || $z=='green') {  echo 'correct';}else {  echo 'incorrect';}

Which one is correct and which one is incorrect? Remember that IF statements only check if something is true or false. These both mean the same thing; if x is yellow and y is purple or z id green, then the statement is true. But does it check x and y first or y and z first? Let's plug it in and find out.

correctincorrect

The answer is that the first statement is true, but the second statement is false. The 'and' is higher than 'or' but lower than '||' on the food chain.

In the first statement, PHP checks x and y first and returns false since both x and y are wrong. Then it compares the z statement, which is true. Then it processes the 'or'. So in the end it turns out to if false 'or' true, then the IF statement is true.

In the second statement, PHP checks y and z first and returns false since y is false even though z is true. Then it compares the x statement, which is false. Finally it processes the 'and'. So in the end it turns out to if false 'and' false, then the IF statement is false.

For all you university level math majors, the first statement really says ($x=='yellow' and $y=='purple') or $z=='green' and the second statement says $x=='yellow' and ($y=='purple' or $z=='green').

How's that food for thought? Take some time and process this new chunk of information. Class is over for today so go out and enjoy the sun. See you guys next week!

Posted by on . Category: PHP


Comments

No comments posted yet

You need to register or login to post new comments.