3

In drupal|menu.inc, I found the constant was defined in Hexadecimal :

define('MENU_IS_ROOT', 0x0001)

why not

define('MENU_IS_ROOT', 1)

==================================

there is another code snippet:

define('MENU_VISIBLE_IN_BREADCRUMB', 0x0004);
define('MENU_SUGGESTED_ITEM', MENU_VISIBLE_IN_BREADCRUMB | 0x0010);

is it equal MENU_SUGGESTED_ITEM = MENU_VISIBLE_IN_BREADCRUMB = 16 ?

ActHtml
  • 27
  • 5
  • The first question is not constructive, but the second question has a definitive answer. Voting to reopen. – Ja͢ck Dec 27 '12 at 12:12

4 Answers4

3

It's for bitwise operations

You can do something like this:

<?php

define("FLAG_ONE", 0x0001);
define("FLAG_TWO", 0x0002);
define("FLAG_THREE", 0x0004);
define("FLAG_FOUR", 0x0008);
define("FLAG_ALL", FLAG_ONE|FLAG_TWO|FLAG_THREE|FLAG_FOUR);

function make_waffles()
{
    echo 'Yummy! We Love Waffles!!!';
}

function do_something($flags)
{
    if ($flags & FLAG_TWO)
       make_waffles();
}

$flags |= FLAG_TWO;
do_something($flags);

?>

BTW, you can check this answer to know when it's better to use const or define.

Community
  • 1
  • 1
Viacheslav Kondratiuk
  • 8,493
  • 9
  • 49
  • 81
1

Hex constants are often used for bit masks. When you're defining all the values, it makes it easy to see the bit pattern relationships.

The resulting values are the same, it just makes the code easier to read.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

AFAIK, the results are the same.
I think it's only a matter of personal preference!

Omid Kamangar
  • 5,768
  • 9
  • 40
  • 69
0

Why not define('MENU_IS_ROOT', 1) instead of define('MENU_IS_ROOT', 0x001)?

Those are identical; some people just like the idea of hexadecimal values.

Is define('MENU_SUGGESTED_ITEM', MENU_VISIBLE_IN_BREADCRUMB | 0x0010); the same as define('MENU_SUGGESTED_ITEM', 16);?

No, 0x0004 | 0x0010 is equivalent to 4 | 16 which equals 20. It's easier to understand the binary or (|) operator when you look at the values in binary:

0x0004 = 00000100
0x0010 = 00010000
         -------- OR
         00010100 = 0x0014 = 20
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309