Re: Inspiration for a Tombstone.
| From: | Colin Guthrie | Date: | Sun, 29 Jun 2008 10:27:38 +0000 |
| Subject: | Re: Inspiration for a Tombstone. | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-276104@lists.php.net to get a copy of this message | ||
Dotan Cohen wrote:
Indeed. The technique obviously only works for constants, and it wont help with variable -> variable comparisons (unless you do something really stupid like put the first expression in quotes!). Using != when possible is a good idea but I guess you have to draw the line as to moving your preferred "flow" of logic around to fit in with a technique for reducing the possibility of logical errors. Even your example above hints at another "political" minefield - Early Return or Multiple Return Points.... (s/Return/Exit/ in this case) Let's not even go there!!! ColAs far as the programming practice that Colin was advocating, it is not a bad habit. And as far as the computer is concerned, the efficiency is a wash since the number of internal steps probably doesn't change much. However, it doesn't always work. (I know - no one claimed it did.) <?php if ($challenge_password_hash = $stored_password_hash) { echo 'Welcome to the club!'; } else { echo 'Stay out! This club is for members only!'; } ?> AndrewIn these instances you could rely on != behaviour instead of == behaviour, like this: <?php if ($challenge_password_hash != $stored_password_hash) { echo 'Stay out! This club is for members only!'; } else { echo 'Welcome to the club!'; } ?> or, better yet: <?php if ($challenge_password_hash != $stored_password_hash) { echo 'Stay out! This club is for members only!'; exit; } echo 'Welcome to the club!'; // Lots of code here that just saved itself another indent in my IDE ?>