13

In the last line of the following code, it has &2, if($page['special']&2).

What does & mean?

if(isset($_REQUEST['id']))$id=(int)$_REQUEST['id'];
else $id=0;
if($id){ // check that page exists
    $page=dbRow("SELECT * FROM pages WHERE id=$id");
    if($page!==false){
        $page_vars=json_decode($page['vars'],true);
        $edit=true;
    }
}
if(!isset($edit)){
    $parent=isset($_REQUEST['parent'])?(int)$_REQUEST['parent']:0;
    $special=0;
    if(isset($_REQUEST['hidden']))$special+=2;
    $page=array('parent'=>$parent,'type'=>'0','body'=>'','name'=>'','title'=>'','ord'=>0,'description'=>'','id'=>0,'keywords'=>'','special'=>$special,'template'=>'');
    $page_vars=array();
    $id=0;
    $edit=false;
}

// { if page is hidden from navigation, show a message saying that
if($page['special']&2)echo '<em>NOTE: this page is currently hidden from the front-end navigation. Use the "Advanced Options" to un-hide it.</em>';
royhowie
  • 11,075
  • 14
  • 50
  • 67
shin
  • 31,901
  • 69
  • 184
  • 271
  • possible duplicate of [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Gordon Jan 16 '11 at 12:47
  • 2
    In the context of this question & was part of binary math, not a 'by reference' operator – Dmitri Jan 16 '11 at 13:27
  • @Dmitri that case is covered in the linked reference as well. – Gordon Jan 16 '11 at 17:21

5 Answers5

16
$page['special'] & 2

means

$page['special'] bitwise AND 2

It basically checks to see if the 2 bit is set in $page['special'].

This line:

if(isset($_REQUEST['hidden']))$special+=2;

explicitly adds 2 to $special so that it'll satisfy the bitwise AND operation and comparison, because decimal 2 == binary 10, with the 1 representing the 21 bit, ensuring it is set.

The AND operation returns 2 if the 2 bit is set, which resolves to true in PHP and satisfies the condition; otherwise it returns 0 which is considered false.

Quite a neat trick IMO, not sure how secure it is though.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 2
    Why would you want that instead of a simple, readable `&& isset($_REQUEST['hidden']);`. I may be tired now but i don't see the advantage. – alexn Jan 16 '11 at 12:30
  • 1
    @alexn: No idea either. Looks like the developer who wrote that code wanted to feel a little smart. – BoltClock Jan 16 '11 at 12:34
  • @alexn, ͏@BoltClock, The advantage is exactly the opposite of what you've quoted. Obfuscation is an advantage when you want to read something and at the same time don't want others to read it. – Pacerier Mar 30 '15 at 11:49
  • 1
    `$special+=2` doesn’t necessarly make `$special & 2` true. For example if initially `$special=2`. – zwirbeltier Jul 08 '19 at 11:12
13

& is the bitwise AND operator. The result of a & b are the bits that are equal in a and b.

So in this case $page['special']&2 returns either 0 or 2 depending on the bit pattern of $page['special']:

  **** **** **** **** **** **** **** **X*    // $page['special']
& 0000 0000 0000 0000 0000 0000 0000 0010    // 2
=========================================
  0000 0000 0000 0000 0000 0000 0000 00X0    // $page['special'] & 2
Gumbo
  • 643,351
  • 109
  • 780
  • 844
8

It's the bitwise and operator.

It looks like it's using that particular bit to hide the page.

In case you don't know what the bitwise operator is, consider the value 74 in binary:

0100 1010

If you and this with 2 (0000 0010), you get:

0100 1010
0000 0010
---- ----
0000 0010

or a non-zero (true) value.

The lines:

$special=0;
if(isset($_REQUEST['hidden']))$special+=2;

are setting that bit based on the hidden key.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

In PHP & is the bitwise operator for AND.

So it would AND the binary value of $page['special'] with the binary value of 2, which would be:

0000 0010

So the overall value will either be 2 or 0.

Marcus Whybrow
  • 19,578
  • 9
  • 70
  • 90
0

& is the bitwise AND operator.

& 2 checks whether the second bit in the special field's value is set.

phihag
  • 278,196
  • 72
  • 453
  • 469