Results 1 to 5 of 5

Thread: Explain this syntax

  1. #1
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default Explain this syntax

    I came across this website and plan on going through it, but I got distracted by the line below. It works and evaluates to true.

    I don't understand what is going on inside the if. Specificly the '&'.

    Can one of you guys explain please? Thanks.

    http://www.nyphp.org/content/present...Dintro/gd2.php

    PHP Code:
    if (imagetypes() & IMG_GIF) echo "GIF Support is enabled<br />"

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The result of imagetypes() is a bitset, and & is the bitwise operator AND. The condition is expressing 'if the imagetypes() bitset has the IMG_GIF bit set', relying on PHP's interpretation of 0 as false.

    For example, if we (probably inaccurately; it doesn't matter for the purposes of demonstration) let IMG_PNG = 1, IMG_GIF = 2, IMG_JPEG = 4, then 7 (1 + 2 + 4) expresses all three:
    Code:
    $imagetypes = 1 + 2 + 4;
    // 1bin + 10bin + 100bin = 111bin = 7dec
    
    $imagetypes & IMG_GIF;
    //   111
    // & 010
    // = 010
    
    $imagetypes = 1 + 4;
    // remove GIF support
    // 1bin + 100bin = 101bin = 5dec
    // we could also express this as $imagetypes -= 2
    //   or $imagetypes ^= 2 (^ is XOR)
    // where the operands are composed only of powers of two,
    //   OR is equivalent to +, and XOR to -, but with a lower
    //   bound of 0.
    
    $imagetypes & IMG_GIF;
    //   101
    // & 010
    // = 000
    Last edited by Twey; 01-19-2009 at 10:09 AM. Reason: Fix explanation of +/| vs. -/^
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Thanks Twey.

    So would it be correct to say that the imagetypes() function returns a bitset and the & is checking that the IMG_GIF is represented in the bitset?

    If imagetypes() evalutes to true AND IMG_GIF is in the result of calling imagetypes(), the if will be true?

    I have never seen that syntax before. Are there any common uses for it?

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    If imagetypes() doesn't evaluate to true then it's 0, and 0 & x === 0 for all x. The return value of imagetypes() is not checked directly: don't confuse & (bitwise AND) with && (boolean AND). & is a mathematical operator like + or *, not a logical operator like &&, and it does not exhibit short-circuit behaviour.

    If you were to program at a lower level, you'd see bit manipulation an awful lot. At this level, about all it's useful for is bitsets and performance tricks.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. The Following User Says Thank You to Twey For This Useful Post:

    JasonDFR (01-19-2009)

  6. #5
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Makes perfect sense now Twey. Thanks a lot.

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
  •