Re: Boolean type forced on string assignment inside if statement

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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/

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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux