Re: [PHP] Boolean type forced on string assignment inside if statement

2013-01-02 Thread Jim Lucas

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."".$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



Re: [PHP] Boolean type forced on string assignment inside if statement

2013-01-02 Thread Jim Lucas

On 01/02/2013 07:53 AM, Marc Guay wrote:

Hi folks,

if ($a = "foo"&&  $b = "bar"){
 echo $a."".$b;
}

Returns:
1
bar

I expect:
foo
bar

Is this documented?

Marc



Why would you do this.

I cannot envision a time or condition that would require such a "test".

Can you please explain why you would want to do this?

Won't this type of condition/test always return true?

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



[PHP] Re: Boolean type forced on string assignment inside if statement

2013-01-02 Thread Tim Streater
On 02 Jan 2013 at 16:05, Stephen  wrote: 

> On 13-01-02 10:53 AM, Marc Guay wrote:
>> Hi folks,
>>
>> if ($a = "foo" && $b = "bar"){
>>  echo $a."".$b;
>> }
>>
>> Returns:
>> 1
>> bar
>>
>> I expect:
>> foo
>> bar
>>
>> Is this documented?
>>
>
> && takes precedence over =
>
> http://php.net/manual/en/language.operators.precedence.php
>
> You may want to use brackets

OP may want to avoid doing something unusual which may confuse a casual reader.

--
Cheers  --  Tim

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

Re: [PHP] Boolean type forced on string assignment inside if statement

2013-01-02 Thread Stephen

On 13-01-02 10:53 AM, Marc Guay wrote:

Hi folks,

if ($a = "foo" && $b = "bar"){
 echo $a."".$b;
}

Returns:
1
bar

I expect:
foo
bar

Is this documented?



&& takes precedence over =

http://php.net/manual/en/language.operators.precedence.php

You may want to use brackets

--
Stephen


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