Looking for caveats to the following code

From: Date: Thu, 17 Aug 2006 22:19:09 +0000
Subject: Looking for caveats to the following code
Groups: php.general 
Request: Send a blank email to php-general+get-240834@lists.php.net to get a copy of this message
Hello, While experimenting with some object stuff I stumbled upon something new (although not object related). Normally I would do this: <?php function do_something($input) { if($input == 'hello') { return $input; } else { return false; } } $result = do_something('hello'); if($result !== false) { // do something with $result } else { // do some other stuff } ?> Using the same function above I discovered I can do this: <?php if($result = do_something('hello')) { // do something with $result } else { // do some other stuff } ?> The issue is whether or not this is a safe test. My initial thought is that it is safe since I'm simply checking for true/false-ness. I either check for '!== false' explicitly or (in the case of the latter example) check that something other than 'false' is returned. It's slightly less readable but it seems more efficient (if nothing more than to save on the number of lines typed). Thoughts? Chris.

« previous php.general (#240834) next »