Dotan Cohen wrote:
As 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!';
}
?>

Andrew


In 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
?>

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!!!


Col


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to