php-general Digest 3 Jan 2013 03:17:46 - Issue 8084
Topics (messages 319993 through 320002):
Boolean type forced on string assignment inside if statement
319993 by: Marc Guay
319994 by: Marc Guay
319995 by: Stephen
319996 by: Tim Streater
319997 by: Jim Lucas
319998 by: Marc Guay
31 by: Jim Lucas
32 by: Marc Guay
320001 by: Marc Guay
320002 by: tamouse mailing lists
Administrivia:
To subscribe to the digest, e-mail:
php-general-digest-subscr...@lists.php.net
To unsubscribe from the digest, e-mail:
php-general-digest-unsubscr...@lists.php.net
To post to the list, e-mail:
php-gene...@lists.php.net
--
--- Begin Message ---
Hi folks,
if ($a = "foo" && $b = "bar"){
echo $a."".$b;
}
Returns:
1
bar
I expect:
foo
bar
Is this documented?
Marc
--- End Message ---
--- Begin Message ---
Interesting that if I wrap them up tighter in braces I get the desires result..
if (($a = "foo") && ($b = "bar")){
echo $a."".$b;
}
Returns
foo
bar
--- End Message ---
--- Begin Message ---
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
--- End Message ---
--- Begin Message ---
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
--- End Message ---
--- Begin Message ---
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/
--- End Message ---
--- Begin Message ---
> 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
--- End Message ---
--- Begin 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."".$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/
--- End Message ---
--- Begin Message ---
Thanks for the head's up. In this case the string will be an English
sentence when it's populated properly so that won't be a concern.
Marc
--- End Message ---
--- Begin Message ---
Something else that's happening with this, which makes it a Bad Idea
(tm) is that when the operator is "or", as it is in my real life
scenerio, the 2nd variable occasionally doesn't get populated if the
first one returns true.
if ($a = "foo" || $b = "bar"){
echo $a."".$b;
}
Returns
foo
And even worse, because I have this in a loop, what can happen is that
if $b gets populated on one loop, it doesn't get reset for the next
one so the data gets seriously bungled.
Moral of the story: Don't be so fancy on your first day back after
vacation. :)
Marc
--- End Message ---
--- Begin Message ---
On Wed, Jan 2, 2013 at 1:02 PM, Marc Guay wrote:
> Something else that's happening with this, which makes it a Bad Idea
> (tm) is that when the operator is "or", as it is in my real life
> scenerio, the 2nd variable occasionally doesn't get populated if the
> first one returns true.
>
> if ($a = "foo" || $b = "bar"){
> echo $a."".$b;
> }
>
> Returns
> foo
>
> And even worse, because I have this in a loop, what can happen is that
> if $b gets populated on one loop, it doesn't get reset for the next
> one so the data gets seriously bungled.
Ah, yeah, seriously, don't mix assignments and tests up this way
unles