FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » A little tip
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: A little tip [message #177511 is a reply to message #177454] Tue, 03 April 2012 13:52 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
Shelly wrote:

> On 3/30/2012 4:30 PM, Thomas Mlynarczyk wrote:
>> Shelly schrieb:
>>> $val = TRUE;
>>
>>> $val &= TRUE;
>>>
>>> which is identical to
>>>
>>> $val = $val && TRUE;
>>
>> No. &= is a bitwise operator while && means the logical AND operator. So
>> when you do $val &= TRUE, PHP is trying to do TRUE & TRUE, but since
>> bitwise operators only work with integers, TRUE is first coerced into
>> the integer 1. So the operation ends up as 1 & 1 yielding the integer 1.
>> Thus, $val = 1. The === operator checks the type of the operands which
>> are int and bool and thus not the same, while == tries to convert the
>> operands into something "comparable" and since they are both "truish"
>> the result is TRUE here.
>
> Thank you. That makes a lot of sense. So, what I have to do if I want
> to use === is to use the expanded form
>
> $val = $val && TRUE;

You keep missing the point. That is _not_ "the expanded form" of anything.
`&=' and `&&' are *different* operators; the first one is a *bitwise*
operator, the second is a *logical* (boolean) one.

In that sense,

x = x & 1;

is "the expanded form" of

x &= 1;

<http://php.net/operators>

Please do not full-quote.


PointedEars
--
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
-- Richard Cornford, comp.lang.javascript, 2011-11-14
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: session variable doesn't appear in side bar
Next Topic: Doubt regarding an array of references...
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 22:05:56 GMT 2024

Total time taken to generate the page: 0.04296 seconds