Re: Boolean Assignment Operator

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

 



On Mon, 2009-02-02 at 23:49 +1100, Gavin Hodge 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?

Operators are listed here:

http://www.php.net/manual/en/language.operators.precedence.php

The compound assignment bitwise operator isn't documented explicitly but
the general idea can be found here:

http://www.php.net/manual/en/language.operators.assignment.php

The === operator is for mathcing values AND data type. By performing a
bitwise & you are returning data of type integer. As such comparing
against boolean true fails when you use ===. Instead what you should do
is the following (both for logic and succinctness):

<?php

$success  = 1;
$success &= operation1();
$success &= operation2();

if( $success )
{
    operation3(); // depends on 1 and 2 being successful
}

?>

Alternatively you can do the following:

<?php

if( 1 & operation1() & operation2() )
{
    operation3(); // depends on 1 and 2 being successful
}

?>

But really what you probably* want is the following:

<?php

if( operation1() && operation2() )
{
    operation3(); // depends on 1 and 2 being successful
}

?>

Why is the last one the one you probably want? Because if operation1()
fails then operation2() won't be evaluated at all... this is usually
what you want, but sometimes running operation2() is required to change
some state someplace, in which case you can use:

<?php

if( operation1() & operation2() )
{
    operation3(); // depends on 1 and 2 being successful
}

?>

Of course bitwise only works if they return the same bitfields. For
instance if operation1() returns 1, and operation2() returns 2, then the
bitwise anding of the values would be false since different bit
positions are being combined.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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