Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between define('MENU_IS_ROOT', 0x0001) and define('MENU_IS_ROOT', 1)?

Tags:

php

drupal

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 ?

like image 376
ActHtml Avatar asked Nov 25 '25 12:11

ActHtml


2 Answers

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.

like image 167
Viacheslav Kondratiuk Avatar answered Nov 27 '25 02:11

Viacheslav Kondratiuk


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.

like image 32
Barmar Avatar answered Nov 27 '25 00:11

Barmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!