Possible Duplicates:
PHP – and / or keywords
PHP shorthand syntax
Quick question. I keep seeing shorthand expressions in libraries around the place, and despite having been a PHP developer for over 3 years, I struggle to quite see how the following would evaluate.
What exactly does the PHP interpreter do with the following shorthand lines of code when it encounters them?
<?php
defined('foo') OR exit('foo is not defined!');
$foo AND $this->bar();
I’m guessing that it’ obviously conditional execution – i.e. the second statement won’t get executed unless the first bit is true… but the use of the bitwise operators confuse me a bit.
Can someone elaborate?
Thanks 🙂
Back when I was learning PHP I remember reading the ‘do or die’ style commands and not fully understanding them at the time, the classic example is:
Bear in mind that the conditions will only run if they’re required, for example:
Here b == c will only be tested if a == b. The same theory applies to or:
Now b == c will only be tested if a != b.
In this case you are relying on this order to run a command (exit or $this->bar()) in certain conditions.
Be Aware… exit() is a bad idea in this circumstance – if you exit(‘something went wrong’) there’s nothing anyone can do to hide this error from the user, also it’s likely to issue a 200 OK HTTP status where a 500 Internal Server Error would be much more appropriate, I would consider something more like:
Here you have a chance to either catch the Exception or at least let PHP catch it and issue a 500 status.
Mat