Professional Web Applications Themes

operator |= - PHP Development

hello, what does the operator |= mean? I saw it in a script : $output = "png"; $style = BCS_ALIGN_CENTER; $style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0; $style |= ($output == "jpeg") ? BCS_IMAGE_JPEG : 0; thanks...

  1. #1

    Default operator |=

    hello,

    what does the operator |= mean?

    I saw it in a script :

    $output = "png";

    $style = BCS_ALIGN_CENTER;
    $style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0;
    $style |= ($output == "jpeg") ? BCS_IMAGE_JPEG : 0;


    thanks


    ··········· sylvain Guest

  2. #2

    Default Re: operator |=

    Hi,

    ··········· sylvain wrote:
    >
    > what does the operator |= mean?
    >
    > I saw it in a script :
    >
    > $output = "png";
    >
    > $style = BCS_ALIGN_CENTER;
    > $style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0;
    > $style |= ($output == "jpeg") ? BCS_IMAGE_JPEG : 0;
    I assume this is the same as the Perl operator, in which case it means,
    if the left hand side isn't true, set it to the right hand side. Useful
    if you want to set up defaults.

    # If $a isn't set, set it to the string "undefined"
    $a |= "undefined";

    Regards,

    Luke

    Luke Ross Guest

  3. #3

    Default Re: operator |=

    Luke Ross wrote:
    > Hi,
    >
    > ··········· sylvain wrote:
    >
    >>
    >> what does the operator |= mean?
    >>
    >> I saw it in a script :
    >>
    >> $output = "png";
    >>
    >> $style = BCS_ALIGN_CENTER;
    >> $style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0;
    >> $style |= ($output == "jpeg") ? BCS_IMAGE_JPEG : 0;
    >
    >
    > I assume this is the same as the Perl operator, in which case it means,
    > if the left hand side isn't true, set it to the right hand side. Useful
    > if you want to set up defaults.
    >
    > # If $a isn't set, set it to the string "undefined"
    > $a |= "undefined";
    >
    > Regards,
    >
    > Luke
    >
    I think you correctly answered the wrong question. He was asking what
    the |= operator is doing, not the ?/: trinary operator. The trinary
    operator in question does the same as the Perl conditional assignment
    operator.

    However, the |= operator is the unary equivalent of the bitwise OR. It
    means the same thing as:

    $lhs = $lhs | $rhs

    So, this code:

    <?
    $lhs = 6;
    $rhs = 13;
    $lhs |= $rhs;
    echo $lhs;
    ?>

    Would display:

    15

    (Since 00000110 | 00001101 = 00001111)

    The code he found is probably a streamlined way of conditionally setting
    a binary sequence (or set of flags) within a preexisting bitfield. The
    binary operator doesn't have any _expected_ effect on strings (notice
    how the results of the expressions within the trinary operator are
    probably both boolean [or a bitfield in the case of the second
    operand--a full-out guess based on the fact that the second operand is a
    "macroscopic" definition]).

    The trinary operator is also interesting in this example, though.

    $style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0;

    Says this:

    1. Check if the variable "$output" is equivalent to the constant "png"
    2. If this expression is true, return the results of the definition
    "BCS_IMAGE_PNG" to the outer expression...
    3. If this expression is false, return a constant 0 to the outer
    expression...
    4. The resulting macro or '0' is sent to the unary operator which
    updates the variable "$style" using the bitwise OR.

    If $output contains the string "png", and BCS_IMAGE_PNG is set to 1,
    then set the low bit of the $style bitfield (makes the number odd and
    adds 1 to the value, if it was even, _or_ just leaves the number odd and
    maintains its value if it was already odd). The nice thing about this
    method is that if $style already has some information in the bitfield, a
    bitwise OR with "0" will not affect its contents.

    The $style string is probably processed through a bit mask to extract
    individual values from the field:

    <?
    function is_low_bit_set($bitfield) {
    return($bitfield & 1);
    }
    ?>

    This says that if the low bit is set, return a 1, otherwise, return a 0.
    That means that any odd, integer will return a 1, and any even integer
    will return a 0.

    HTH,
    Zac

    Zac Hester Guest

  4. #4

    Default Re: operator |=

    On Fri, 19 Sep 2003 10:48:18 +0200, "···········sylvain"
    <sylvainantispamsylvax.net> wrote:
    >what does the operator |= mean?
    >
    >I saw it in a script :
    >
    >$output = "png";
    >
    >$style = BCS_ALIGN_CENTER;
    >$style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0;
    >$style |= ($output == "jpeg") ? BCS_IMAGE_JPEG : 0;
    $x |= $y is equivalent to $x = $x | $y

    | is bitwise-or.

    [url]http://uk2.php.net/manual/en/language.operators.bitwise.php[/url]

    e.g.

    <pre>
    <?php
    printf("1 = %03b\n", 1);
    printf("6 = %03b\n", 6);
    printf("1 | 6 = %03b\n", 1|6);
    ?>
    </pre>

    Outputs:

    1 = 001
    6 = 110
    1 | 6 = 111

    --
    Andy Hassall (andyandyh.co.uk) icq(5747695) ([url]http://www.andyh.co.uk[/url])
    Space: disk usage ysis tool ([url]http://www.andyhsoftware.co.uk/space[/url])
    Andy Hassall Guest

  5. #5

    Default Re: operator |=

    More specifically,
    $a |= $b;
    is the same as
    $a = $a || $b;

    [url]http://www.php.net/manual/en/language.operators.php[/url]
    hey, look at that... a whole section of the manual dedicated to operators.

    Luke Ross <lukerosssys3175.co.uk> wrote in message news:<vmli6itl5sfd44corp.supernews.com>...
    > Hi,
    >
    > ··········· sylvain wrote:
    > >
    > > what does the operator |= mean?
    > >
    > > I saw it in a script :
    > >
    > > $output = "png";
    > >
    > > $style = BCS_ALIGN_CENTER;
    > > $style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0;
    > > $style |= ($output == "jpeg") ? BCS_IMAGE_JPEG : 0;
    >
    > I assume this is the same as the Perl operator, in which case it means,
    > if the left hand side isn't true, set it to the right hand side. Useful
    > if you want to set up defaults.
    >
    > # If $a isn't set, set it to the string "undefined"
    > $a |= "undefined";
    >
    > Regards,
    >
    > Luke
    BKDotCom Guest

  6. #6

    Default Re: operator |=

    On 19 Sep 2003 13:46:25 -0700, [email]BradKentswbell.net[/email] (BKDotCom) wrote:
    >More specifically,
    >$a |= $b;
    >is the same as
    >$a = $a || $b;
    No, it's not.

    $a |= $b is $a = $a | $b

    $a | $b is bitwise OR.
    $a || $b is logical OR.

    <pre>
    <?php
    var_dump(1 | 4);
    var_dump(1 || 4);
    ?>
    </pre>

    Outputs:

    int(5)
    bool(true)

    --
    Andy Hassall (andyandyh.co.uk) icq(5747695) ([url]http://www.andyh.co.uk[/url])
    Space: disk usage ysis tool ([url]http://www.andyhsoftware.co.uk/space[/url])
    Andy Hassall Guest

  7. #7

    Default operator |=

    hello,

    what does the operator |= mean?

    I saw it in a script :

    $output = "png";

    $style = BCS_ALIGN_CENTER;
    $style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0;
    $style |= ($output == "jpeg") ? BCS_IMAGE_JPEG : 0;


    thanks


    ··········· sylvain Guest

  8. #8

    Default Re: operator |=

    Hey

    It's bitwise operator OR
    Code below could be interpreter as

    $style = BCS_ALIGN_CENTER;
    if ( $output == "png" )
    {
    $style = $style | BCS_IMAGE_PNG;
    }
    if ( $output == "jpeg" )
    {
    $style = $style | BCS_IMAGE_JPEG;
    }

    "···········sylvain" <sylvainantispamsylvax.net> wrote in message
    news:bkmh17$kqg$1news-reader2.wanadoo.fr...
    > hello,
    >
    > what does the operator |= mean?
    >
    > I saw it in a script :
    >
    > $output = "png";
    >
    > $style = BCS_ALIGN_CENTER;
    > $style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0;
    > $style |= ($output == "jpeg") ? BCS_IMAGE_JPEG : 0;
    >
    >
    > thanks
    >
    >

    Dmitry Ruban Guest

Similar Threads

  1. operator
    By Gyger in forum PHP Development
    Replies: 3
    Last Post: August 18th, 07:18 AM
  2. XOR operator?
    By Daniel Carrera in forum Ruby
    Replies: 9
    Last Post: October 29th, 12:16 AM
  3. or operator
    By Paul Kraus in forum PERL Beginners
    Replies: 0
    Last Post: August 29th, 05:58 PM
  4. k operator in REGEX??
    By darkname in forum PERL Miscellaneous
    Replies: 6
    Last Post: July 7th, 09:18 AM
  5. BETWEEN OPERATOR
    By LPT in forum Microsoft SQL / MS SQL Server
    Replies: 0
    Last Post: July 3rd, 05:14 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139