RE: [PHP] Boolean Assignment Operator
| From: | Steven Buehler | Date: | Mon, 02 Feb 2009 14:07:42 +0000 |
| Subject: | RE: [PHP] Boolean Assignment Operator | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-287536@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: farnion@googlemail.com [mailto:farnion@googlemail.com] On Behalf
> Of Edmund Hertle
> Sent: Monday, February 02, 2009 7:03 AM
> To: Gavin Hodge
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] Boolean Assignment Operator
>
> 2009/2/2 Gavin Hodge <gavin.hodge@gmail.com>
>
> > Hi,
> >
> > I'm fairly new to PHP, having migrated from the Java / C# world.
> >
> > I wrote some code similar to the following:
> >
> > $success = true;
> > $success &= operation1();
> > $success &= operation2();
> >
> > if ($success === true) {
> > operation3(); // depends on 1 and 2 being successful
> > }
> >
> > This didn't work as expected. After a bit of digging I found:
> > * The &= operation isn't mentioned anywhere in the PHP documentation
> > * The &= function seems to work as expected, however the following is
> > observed...
> > $success = true;
> > $success &= true;
> > print $success == true; // outputs 1
> > print $sucesss === true; // no output
> > * The 'or' assignment operator |= causes no errors but doesn't work.
> >
> > Can any PHP gurus explain why the &= operator works at all, and why
> > === seems to fail afterwards?
> >
> > Cheers,
> > Gavin.
>
>
> Hey,
>
> never heard of the "|=" operator. So I think php does not support it.
> I cannot say how "&=" works in Java or C# but as of php it works like
> that
> (IMO) (reference instead of copy):
> $var1 = "test1";
> $var2 = $var1;
> $var3 &= $var1;
> $var1 = "test2";
>
> echo $var1; // "test2"
> echo $var2; // "test1"
> echo $var3; // "test2"
Actually, if you run your test, $var3 does NOT come out as "test2", it will return a
"0" for that echo statement. To get the results you want, it needs to be:
$var3 =& $var1;