Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #5338

Re: A little tip

Message-ID <20840717.cCmZMDQWIF@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-04-03 15:52 +0200
Subject Re: A little tip
Newsgroups comp.lang.php
References <jl4ovl$h2i$1@dont-email.me> <jl4q1i$top$1@dont-email.me> <jl4te2$hi5$1@dont-email.me> <jl554d$1s6$1@news.albasani.net> <jl56kh$6ku$1@dont-email.me>
Followup-To comp.lang.php

Followups directed to: comp.lang.php

Show all headers | View raw


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

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

A little tip Shelly <sheldonlg@thevillages.net> - 2012-03-30 13:02 -0400
  Re: A little tip Jerry Stuckle <jstucklex@attglobal.net> - 2012-03-30 13:20 -0400
    Re: A little tip Shelly <sheldonlg@thevillages.net> - 2012-03-30 14:18 -0400
      Re: A little tip Jerry Stuckle <jstucklex@attglobal.net> - 2012-03-30 16:19 -0400
      Re: A little tip Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2012-03-30 22:30 +0200
        Re: A little tip Doug Miller <doug_at_milmac_dot_com@example.com> - 2012-03-30 20:55 +0000
        Re: A little tip Shelly <sheldonlg@thevillages.net> - 2012-03-30 16:55 -0400
          Re: A little tip Jerry Stuckle <jstucklex@attglobal.net> - 2012-03-30 17:07 -0400
          Re: A little tip Jerry Stuckle <jstucklex@attglobal.net> - 2012-03-31 10:05 -0400
            Re: A little tip Shelly <sheldonlg@thevillages.net> - 2012-03-31 11:27 -0400
          Re: A little tip Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-03 15:52 +0200
            Re: A little tip Shelly <sheldonlg@thevillages.net> - 2012-04-03 10:20 -0400
    Re: A little tip Doug Miller <doug_at_milmac_dot_com@example.com> - 2012-03-30 19:36 +0000
      Re: A little tip Jerry Stuckle <jstucklex@attglobal.net> - 2012-03-30 16:21 -0400
        Re: A little tip Doug Miller <doug_at_milmac_dot_com@example.com> - 2012-03-30 20:52 +0000
          Re: A little tip Jerry Stuckle <jstucklex@attglobal.net> - 2012-03-30 17:08 -0400
            Re: A little tip Shelly <sheldonlg@thevillages.net> - 2012-03-30 17:22 -0400
              Re: A little tip "Mr. B-o-B" <mr.chew.baka@gmail.com> - 2012-03-30 18:00 -0500
                Re: A little tip Scott Johnson <noonehome@chalupasworld.com> - 2012-03-30 20:33 -0700
          Re: A little tip Shelly <sheldonlg@thevillages.net> - 2012-03-30 17:20 -0400
            Re: A little tip Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-03 15:56 +0200
              Re: A little tip Shelly <sheldonlg@thevillages.net> - 2012-04-03 10:20 -0400
          Re: A little tip Michael Fesser <netizen@gmx.de> - 2012-03-31 05:04 +0200
            Re: A little tip Jerry Stuckle <jstucklex@attglobal.net> - 2012-03-30 23:31 -0400
          Re: A little tip Scott Johnson <noonehome@chalupasworld.com> - 2012-03-30 20:35 -0700
            Re: A little tip Jerry Stuckle <jstucklex@attglobal.net> - 2012-03-30 23:53 -0400
              Re: A little tip Doug Miller <doug_at_milmac_dot_com@example.com> - 2012-03-31 11:58 +0000
                Re: A little tip Jerry Stuckle <jstucklex@attglobal.net> - 2012-03-31 09:05 -0400
  Re: A little tip "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2012-04-02 09:42 +0200
    Re: A little tip "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-04-02 10:22 +0200
      Re: A little tip Shelly <sheldonlg@thevillages.net> - 2012-04-02 08:15 -0400
        Re: A little tip "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-04-02 14:27 +0200
          Re: A little tip Shelly <sheldonlg@thevillages.net> - 2012-04-02 09:40 -0400
            Re: A little tip "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-04-02 23:43 +0200

csiph-web