I find myself doing this kind of thing somewhat often:
$foo = true;
$foo = $foo && false; // bool(false)
With bitwise operators, you can use the &=
and |=
shorthand:
$foo = 1;
$foo &= 0; // int(0)
Given that bitwise operations on 1
and 0
are functionally equivalent to boolean operations on true
and false
, we can rely on type-casting and do something like this:
$foo = true;
$foo &= false; // int(0)
$foo = (bool)$foo; // bool(false)
...but that's pretty ugly and defeats the purpose of using a shorthand assignment syntax, since we have to use another statement to get the type back to boolean.
What I'd really like to do is something like this:
$foo = true;
$foo &&= false; // bool(false)
...but &&=
and ||=
are not valid operators, obviously. So, my question is - is there some other sugary syntax or maybe an obscure core function that might serve as a stand-in? With variables as short as $foo
, it's not a big deal to just use $foo = $foo && false
syntax, but array elements with multiple dimensions, and/or object method calls can make the syntax quite lengthy.
$some['big']['long']['variable'] = $some['big']['long']['variable'] && $some['other']['boolean'];
). – Oersted$foo &= false;
and$foo &&= false;
for$foo = true
. So I'm failing to see the problem/goal. – Philosophyint(0)
and the latter yieldsbool(false)
. So while0 == false
because of implicit type-casting,0 !== false
. – Oersted$mailsSent = (Swift_Mailer)$mailer->send($message)
isfalse
, if mail not sent, I've been using$mailsSent &= $mailer->send($message2)
consecutively. After a year of such usage it was the first time, that I had different number of recipients in first and second mail group. Today I found out two things. First if mails are successfully sentsend()
method returns the number of mails sent and nottrue
. Second&=
is a bitwise assignment operator, meaning1 + 2 = 0
. Should've read the docs more thoroughly. Solved it with(bool)$mailer->send($message)
. – Somatotype