Re: Boolean type forced on string assignment inside if statement
| From: | Tedd Sperling | Date: | Thu, 03 Jan 2013 17:16:04 +0000 |
| Subject: | Re: Boolean type forced on string assignment inside if statement | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-320010@lists.php.net to get a copy of this message | ||
On Jan 3, 2013, at 11:49 AM, Marc Guay <marc.guay@gmail.com> wrote:
> I just ran this:
>
> if (($a = "foo") || ($b = "bar")){
> echo $a."<br />".$b;
> }
>
> and it only spat out "foo" so I'm guessing things have changed. :)
>
> Marc
Marc et al:
I joined late into this conversation, so I may be missing the point, but you want to discus
strangeness try this:
<?php
if (($a = 'foo') | ($b = 'bar')) // <-- note the single pipe ( | )
{
echo "$a <br > $b";
}
else
{
echo 'Neither are populated';
}
?>
However, the above practice of using one '=' is questionable -- the following is better.
<?php
$a = 'foo';
$b = 'bar';
if (($a == 'foo') | ($b == 'bar'))
{
echo "$a <br > $b";
}
else
{
echo 'Neither are populated';
}
?>
Comment out the variables to see how things work. Also change the number of pipes to see how things
change.
To the more accomplished programmers reading this, here's a question:
What's the difference between using one pipe or two in an 'if' statement? :-)
Cheers,
tedd
_____________________
tedd@sperling.com
http://sperling.com