0

I found this bunch of code in Services_JSON php library:

if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
    $stk = array(SERVICES_JSON_IN_OBJ);
    $obj = array();
} else {
    $stk = array(SERVICES_JSON_IN_OBJ);
    $obj = new stdClass();
}

My question is about the first line. What does the single "&" operator do?

Cœur
  • 37,241
  • 25
  • 195
  • 267
benomite
  • 848
  • 7
  • 23
  • 1
    RTM - http://php.net/manual/en/language.operators.bitwise.php – j08691 Mar 05 '13 at 15:50
  • It is a bitwise operator, not a logical operator. It operates on the data at a binary level, which gives you a lot of power, but please don't confuse it with the logical AND operator, `&&`. – Andrew Mar 05 '13 at 15:52

3 Answers3

4

It's the PHP bitwise AND operator: http://php.net/manual/en/language.operators.bitwise.php

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

&& means "and" (logical operator) but a single "&" is a bitwise operator that does a binary comparison of the bits.

Here is the manual from php.net: http://us2.php.net/manual/en/language.operators.bitwise.php

blairzotron
  • 259
  • 2
  • 7
0

Just to explain not only what & does but what this bitwise-and operator is probably used for in this case take a look at

<?php
define('SOME_FLAG', 1);
define('SERVICES_JSON_LOOSE_TYPE', 2);
define('ANOTHER_FLAG', 4);

foo(0);
foo(7);
foo(SERVICES_JSON_LOOSE_TYPE|ANOTHER_FLAG);

function foo($value) {
    echo "foo($value)\n";

    bar($value, SOME_FLAG);
    bar($value, SERVICES_JSON_LOOSE_TYPE);
    bar($value, ANOTHER_FLAG);

    if ( $value & SOME_FLAG ) {
        echo "  SOME_FLAG set\n";
    }
    else {
        echo "  SOME_FLAG NOT set\n";
    }

    if ( $value & SERVICES_JSON_LOOSE_TYPE ) {
        echo "  SERVICES_JSON_LOOSE_TYPE set\n";
    }
    else {
        echo "  SERVICES_JSON_LOOSE_TYPE NOT set\n";
    }

    if ( $value & ANOTHER_FLAG ) {
        echo "  ANOTHER_FLAG set\n";
    }
    else {
        echo "  ANOTHER_FLAG NOT set\n";
    }
    echo "\n";
} 

function bar($value, $flag) {
    printf("  value|  %08s (%d)\n", decbin($value), $value);
    printf("  flag |& %08s (%d)\n", decbin($flag), $flag);
    printf("  and  |= %08s (%d)\n", decbin($value & $flag), $value & $flag);
    echo "\n";
}

the output is

foo(0)
  value|  00000000 (0)
  flag |& 00000001 (1)
  and  |= 00000000 (0)

  value|  00000000 (0)
  flag |& 00000010 (2)
  and  |= 00000000 (0)

  value|  00000000 (0)
  flag |& 00000100 (4)
  and  |= 00000000 (0)

  SOME_FLAG NOT set
  SERVICES_JSON_LOOSE_TYPE NOT set
  ANOTHER_FLAG NOT set

foo(7)
  value|  00000111 (7)
  flag |& 00000001 (1)
  and  |= 00000001 (1)

  value|  00000111 (7)
  flag |& 00000010 (2)
  and  |= 00000010 (2)

  value|  00000111 (7)
  flag |& 00000100 (4)
  and  |= 00000100 (4)

  SOME_FLAG set
  SERVICES_JSON_LOOSE_TYPE set
  ANOTHER_FLAG set

foo(6)
  value|  00000110 (6)
  flag |& 00000001 (1)
  and  |= 00000000 (0)

  value|  00000110 (6)
  flag |& 00000010 (2)
  and  |= 00000010 (2)

  value|  00000110 (6)
  flag |& 00000100 (4)
  and  |= 00000100 (4)

  SOME_FLAG NOT set
  SERVICES_JSON_LOOSE_TYPE set
  ANOTHER_FLAG set

It's a way to test if a certain flag is set in a value.
Take a look at the php documentation, e.g. the documentation of of preg_match_all:

 flags
    Can be a combination of the following flags [...]
    PREG_PATTERN_ORDER [...]
    PREG_SET_ORDER [...]
    PREG_OFFSET_CAPTURE [...]

You will will find this Can be a combination of the following flags... thing many times in the documentation. And the code behind those functions/methods does exactly the kind of checking using the bitwise-and operator.

VolkerK
  • 95,432
  • 20
  • 163
  • 226