I’m going to start a new tradition today - a simple tip of the day. Each tip will be it’s own thread. Most all will be a single page, but they all will touch on different areas of coding so if they get a thread started on the topic that’s fine. Today’s tip concerns boolean expressions, so questions and comments on them are on topic for the tip. So here goes…
Occasionally I see this in code posted here and elsewhere:
Then you’ll see a ternary operator from programmers who know of it.
$flag = ($var == '1') ? true : false;
But here’s the thing - and the tip of the day. Variables can store the outcome of a boolean expression.
$flag = $var == '1';
The above is functionally equivalent to both the if expression and the ternary operation above. You can use parenthesis if you want but isn’t strictly required. There is no need to use an if statement or ternary operation to find the value and then assign it.
Boolean operations are just another math operation to the computer though their outcome is 1 or 0, which are assigned the constants true and false respectively in most programming languages, including PHP. While associated with control structures, they do not have to be limited to them.
This is also faster in terms of CPU cycles to just assign the boolean outcome directly, though its not a great enough of a difference to be noticeable on most modern systems.
Thank you Michael Morris as this is new to my eyes, very helpful to know the same outcome can be achieved by storing the boolean outcome directly into the variable rather then using extra memory to run a ternary operator.
Will definitely try to use it where i can as im always seeking new expressions to make my scripts smaller and smaller but achieve the same outcome.
Good tip, however, since we are on the topic of tips, another 2 good tips would be to correct the 1 (and only) line in the current tip and use this instead:
$flag = ‘1’ === $var;
(a small tip #3, I would actually write $flag = (‘1’ === $var); just to make it more readable, but that’s up to you)
Remember kids - it’s much better to write ‘1’ === $var than $var == ‘1’
Also, try to make it a habit to use type check also (use === instead of ==)
The 2 equals will be true is $var is integer 1 or string ‘1’.
This is not usually a good enough check.
Make it a habit of checking the type as well as value by using 3 equal signs.
Now, the reason to write ‘1’ === $var is because if you make a mistake and write
something like if($val = $1){deleteAccount() } else { // something else }
The script will actually evaluate to true and delete the account!
This is because you made a mistake and used only 1 equal sign and assigned ‘1’ to $var instead of comparing to see if $var equals 1. The assignment operator evaluates to true, so your condition check passed without any warning.
But when you make a habit of comparing values using the opposite format, like
if(‘1’ == $var), then if you make a mistake and write if(‘1’’ = 1){}
php will complain and stop you in your track.
It’s all about having good coding habits, they will save you a lot of time later.
So remember kids: use === almost always! and always, always compare the value to $var not $var to value!