How php developers choose default constants values? -


by using get_defined_constants function php, can see defined constants system , myself.

example:

<?php  define("my_constant", 1); print_r(get_defined_constants(true));  ?> 

output:

array (     [core] => array         (             [e_error] => 1             [e_recoverable_error] => 4096             [e_warning] => 2             [e_parse] => 4             [e_notice] => 8             [e_strict] => 2048             [e_deprecated] => 8192             [e_core_error] => 16             [e_core_warning] => 32             [e_compile_error] => 64             [e_compile_warning] => 128             [e_user_error] => 256             [e_user_warning] => 512             [e_user_notice] => 1024             [e_user_deprecated] => 16384             [e_all] => 30719             [debug_backtrace_provide_object] => 1             [debug_backtrace_ignore_args] => 2 .... 

question: how did come integer value constants? example e_all has value of 30719. why 30719 , not random number?

this comes down binary:

[e_error] => 1                 //000000000000001 [e_warning] => 2               //000000000000010 [e_parse] => 4                 //000000000000100 [e_notice] => 8                //000000000001000 [e_core_error] => 16           //000000000010000 [e_core_warning] => 32         //000000000100000 [e_compile_error] => 64        //000000001000000 [e_compile_warning] => 128     //000000010000000 [e_user_error] => 256          //000000100000000 [e_user_warning] => 512        //000001000000000 [e_user_notice] => 1024        //000010000000000 [e_strict] => 2048             //000100000000000 [e_recoverable_error] => 4096  //001000000000000 [e_deprecated] => 8192         //010000000000000 [e_user_deprecated] => 16384   //100000000000000 [e_all] => 30719               //111011111111111 (everything e_strict) 

if you'd want e_error , e_user_error, you'd perform bitwise or statement:

define("e_error_all", e_error | e_user_error); 

this same following

000000000000001 //e_error 000000100000000 //e_user_error ------|-------|  000000100000001 //our custom e_error_all 

as or super simple:

0 - 0 = 0 0 - 1 = 1 1 - 0 = 1 1 - 1 = 1 

when checking, can use bitwise and operation, , if result more 0 bit included:

<?php     $errorcode = e_parse | e_core_error;      if (($errorcode & e_parse) > 0) {         echo "error code includes e_parse" . php_eol;     }      if (($errorcode & e_notice) > 0) {         echo "error code includes e_notice" . php_eol;     }      if (($errorcode & e_core_error) > 0) {         echo "error code includes e_core_error" . php_eol;     } ?> 

will output

error code includes e_parse error code includes e_core_error 

demo

the reason works because and uses following logic:

0 - 0 = 0 0 - 1 = 0 1 - 0 = 0 1 - 1 = 1 

therefore, if check our custom error code (10100) against e_core_error (10000), following operation:

    10100 , 10000     |----     10000 = 16 (therefore larger 0) 

but if check e_notice (01000), following operation performed:

    10100 , 01000     -----     00000 = 0 (no match) 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -