Re: Boolean type forced on string assignment inside if statement

From: Date: Wed, 02 Jan 2013 18:38:24 +0000
Subject: Re: Boolean type forced on string assignment inside if statement
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-319999@lists.php.net to get a copy of this message
On 01/02/2013 10:21 AM, Marc Guay wrote:
Won't this type of condition/test always return true?
I simplified the example. In my real life case the "foo" and "bar" are variables which can be empty strings, and if they both are not, I would like to display them. Marc
Then why not use empty()? $a = "foo"; $b = "bar"; if ( !empty($a) && !empty($b) ){
    echo $a."<br />".$b;
} The reason I ask is that what if one of your strings was a "0" (zero) I would expect your code would not work as you had intended. php -r 'if ( $a="foo" && $b="0" ) { echo "\n\n{$a}\n{$b}\n\n"; }' In my testing, it does not. I would then have to ask, how often do you think a string will be "0"? -- Jim Lucas http://www.cmsws.com/ http://www.cmsws.com/examples/

« previous php.general (#319999) next »