SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Never seen this before...

    Hi guys,

    I'm looking through some legacy code created by another 'developer' and I use the term developer very loosely.

    I may be missing the obvious here, but I've come accross the following code and i've never seen the & operator used like it is.
    PHP Code:
    IF( !($obj->status CONSTANT_NAME) ) { ... } 
    What does "$foo & $bar" do?
    Is it sort of an equals operator?

    Also, i've came across a MySQL query containing something similar
    Code:
    (status & ".CONSTANT_1." = 0 AND status & ".CONSTANT_2." = 0)
    What does this one do?...

    I can see myself being very embarrassed very soon...

    Cheers.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Never used it myself, but here the manual talks about it: http://www.php.net/manual/en/languag...rs.bitwise.php

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the link.
    So... It's sort of a comparison?

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not sure what exactly you mean by " I use the term developer very loosely." buy if you don't know what the & means, then you are hardly a "real developer" yourself.
    These types of operators are usually some of the first things they teach in computer science classes and in every programming training courses.

    Do you know what the && operator means? It actually the optimized version of the &, which means it basically works just like the & but will not even try a second evaluation if the first one fails.

    But in that specific case you mentioned it means something like this:
    If the bits of the 'status' also exist in the CONSTANT_NAME then condition is true.

    The 'status' and CONSTANT_NAME don't have to be the same, but all the bits in the CONSTANT_NAME must also be present in the 'status'
    This is a very common way to set option flags and it's extremely fast to compare. If only more developers were using the bitwise flags for options, the world would be a better place


    For example, any odd number & 1 is true, while any even number & 1 is false

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reason I said I use the term loosely is because the guy before my predecessor was appalling based on his code and lack of common sense and planning.

    I use the && operator all the time. I've just never came across the & operator in the past whether it be a tutorial, book or anyone else's code hence my reason for asking what it's purpose is.

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually the && is an "optimized" version of the & operator.
    The optimization is this: if the first parameter evaluates to false, the evaluation immediately returns false and second is never evaluated.
    This makes it possible to do this like this:
    if(is_object($o) && $o->status === GOOD){
    // do stuff
    }

    Here if the $o is not an object the script will never get to $o->status, if it did, it would have resulted in parse error.

    You can also use this optimization trick to your advantage. For example, when you need to evaluate multiple conditions, always put the ones that most likely to fail first.

    The same is true with the || operator, which is an optimized version of the bitwise | operator, only it returns true if the first condition is true.

    This is a very common optimization and it's used in many languages, even the XSLT standards require XSL parsers to use this type of optimization for && and || evaluations.
    JavaScript goes even further, returning the result of the first condition (not just true or false).


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •