Re: Boolean Assignment Operator

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

 



On Mon, Feb 2, 2009 at 7:49 AM, Gavin Hodge <gavin.hodge@xxxxxxxxx> wrote:
> 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.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

http://us.php.net/manual/en/language.operators.bitwise.php

You're doing an AND, then assigning it when you say &=.  Think of it
like doing something such as .= or +=.

You might also be confusing &= with =& where =& means assign by
reference.  This used to be very necessary when dealing with php4 oop,
but isn't so much anymore.

-- 
http://www.voom.me | EFnet: #voom

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