Re: Boolean type forced on string assignment inside if statement

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

 



On Jan 3, 2013, at 11:49 AM, Marc Guay <marc.guay@xxxxxxxxx> 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@xxxxxxxxxxxx
http://sperling.com
-- 
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