PHP or die?
Posted on 2009-11-18 (Updated on 2019-01-24)
In several low-level, beginner PHP tutorials you'll see this:
mysql\_connect() or die('Cannot connect to the database');
First of all, if you're doing this, knock it off. It's a terrible practice, which leaves whoever is using your page completely in the dark. In fact, if you have the die() function AT ALL through ANY production code, stop reading this. Fire up your text editor, and find every line with this function in it. These are bugs in your code. This is why people make fun of PHP.
Now, since that is a terrible way to program, I've never used that line of code before. However, I have used this in functions plenty of times with my database class:
if(!$db->Connect()) { return false; }
From there, my code displays a message to the user and logs an error so that I can address it later. While coding up something similar to the previous code, I had a thought: could I substitute a one-liner that does the same thing?
$db->Connect() or return false;
To see if this would work, I tried looking up in the PHP Documentation. If ever there was a search-proof term, "or" is it, so I ended up manually digging around the documentation. I couldn't find any documentation of this kind of syntax, and I quickly realized why: I should have been looking under Logical Operators.
It turns out, this coding technique was simply using the properties of a short-circuit to make some very elegant code. If the first statement is true, PHP realizes that the whole expression will return true, and therefore doesn't bother executing the second statement.
So, my question is... is it safe? Should this "or" technique be used in important code? The only time I've ever seen this employed is in a piece of code that isn't good. Does that mean the technique itself is a bad idea? It smells to me like using a break statement in the middle of a for loop, but I really think this could be useful. What do you think, internet?
Tags: php
Comments
<p>I'm slowly moving over to PDO as well, but I usually wrap the innards of whatever I'm doing into a class so that I can swap them out when I get API ADD, like I often do. Just rewrite the class, and the other hundreds of pages that use it remain unchanged.<br /> <a href="http://www.ed-hardy.cc/" rel="nofollow">ed hardy</a></p>ed hardy
<p>Jezra,</p> <p>I'm slowly moving over to PDO as well, but I usually wrap the innards of whatever I'm doing into a class so that I can swap them out when I get API ADD, like I often do. Just rewrite the class, and the other hundreds of pages that use it remain unchanged.</p> <p>As for using a break in a for loop... have you ever started falling in a dream, and then woken up? I imagine it's like that, but for the interpreter.</p>Jacob – http://blog.fragdev.com
<p>If it works, then use it.<br /> Although personally, I prefer to abstract the DB connection with PHP Data Objects and use:<br /> try/catch to handle things that can throw an error.</p> <p>Wait a second? What is wrong with using a break statement in a loop?</p>jezra – http://www.jezra.net