Constant Combination¶
PHP combines predefined constants using bitwise operators. This is heavily used with error level constants, file permissions, and various flags.
Combine two constants into one, with the | or operator: E_NOTICE | E_WARNING. This might also be achieved with + operator.
Check if a flag is set, with the & and operator: E_ALL | E_WARNING.
Exclude one flag, with the ~ tilde operator: E_ALL & ~E_WARNING.
Toggle flags, with the ^``xor operator: ``E_ALL ^ E_WARNING.
These combinations are possible when the constants are distinct powers of 2. That way, combining E_WARNING = 2 // 0010 and E_NOTICE = 8 // 1000 gives E_WARNING | E_NOTICE = 10 // 1010 (both bits set).
Constant combination is used with such functions as error_reporting(), phpcredits(), htmlentities(), sort() though not with all constants, etc.
<?php
// Combine multiple error levels
error_reporting(E_NOTICE | E_WARNING | E_ERROR);
// More common: start from ALL and exclude some
error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);
// -1 represents a flag with ALL activated
error_reporting(-1);
?>
Related : Error Reporting, Sort, Bitfield