2

Could someone clarify how to check for flags within a user defined function.

These constants are predefined glob flags.

  • GLOB_BRACE
  • GLOB_MARK
  • GLOB_NOSORT
  • GLOB_NOCHECK
  • GLOB_NOESCAPE
  • GLOB_ERR
  • GLOB_ONLYDIR

and I have created a new one just to test.

define('GLOB_CUSTOM', 123);

I have also tried

define('GLOB_CUSTOM',0b1111011);

The results are the same.

This function does a var_dump of the flags passed.

function flags_test($flags = NULL) {

    echo '$flags argument<br>';
    var_dump($flags); // int 1073746108
    echo '<br>';

    if($flags & GLOB_BRACE){ 
        echo 'FLAG : "GLOB_BRACE" is set';
        var_dump(GLOB_BRACE);
        echo '<br>';
    }
    if($flags & GLOB_MARK){ 
        echo 'FLAG : "GLOB_MARK" is set';
        var_dump(GLOB_MARK);
        echo '<br>';
    }
    if($flags & GLOB_NOSORT){ 
        echo 'FLAG : "GLOB_NOSORT" is set';
        var_dump(GLOB_NOSORT);
        echo '<br>';
    }
    if($flags & GLOB_NOCHECK){ 
        echo 'FLAG : "GLOB_NOCHECK" is set';
        var_dump(GLOB_NOCHECK);
        echo '<br>';
    }
    if($flags & GLOB_NOESCAPE){ 
        echo 'FLAG : "GLOB_NOESCAPE" is set';
        var_dump(GLOB_NOESCAPE);
        echo '<br>';
    }
    if($flags & GLOB_ERR){ 
        echo 'FLAG : "GLOB_ERR" is set';
        var_dump(GLOB_ERR);
        echo '<br>';
    }
    if($flags & GLOB_ONLYDIR){ 
        echo 'FLAG : "GLOB_ONLYDIR" is set';
        var_dump(GLOB_ONLYDIR);
        echo '<br>';
    }
    if($flags & GLOB_CUSTOM){ 
        echo 'FLAG : "GLOB_CUSTOM" is set';
        var_dump(GLOB_CUSTOM);
        echo '<br>';
    }
}

Test one.

flags_test(GLOB_ONLYDIR); // test one

Results

$flags argument
int 168

FLAG : "GLOB_BRACE" is set
int 128

FLAG : "GLOB_MARK" is set
int 8

FLAG : "GLOB_NOSORT" is set
int 32

FLAG : "GLOB_CUSTOM" is set
int 123

Test two.

flags_test(GLOB_CUSTOM);

Results

$flags argument
int 251

FLAG : "GLOB_BRACE" is set
int 128

FLAG : "GLOB_MARK" is set
int 8

FLAG : "GLOB_NOSORT" is set
int 32

FLAG : "GLOB_NOCHECK" is set
int 16

FLAG : "GLOB_CUSTOM" is set
int 123

I have a few questions.

  • In test one why is GLOB_CUSTOM showing as set ?
  • In test two why is GLOB_BRACE,GLOB_MARK,GLOB_NOSORT and GLOB_NOCHECK showing as set ?
  • What does the value of the var_dump($flags) represent(where did that number come from)?

How to implement a bitmask in php? is where i started, i build my example from the accepted answer. Unfortunately it doesn't explain any of the points above.

Edit :

Flags must be powers of 2 in order to bitwise-or together properly.PHP function flags, how?

This should solve the problem

define('GLOB_CUSTOM', 64);
Community
  • 1
  • 1
TarranJones
  • 4,084
  • 2
  • 38
  • 55
  • This won't even run for me. `define('GLOB_CUSTOM', 092364023760); #PHP Parse error: Invalid numeric literal in php shell code on line 1`. Everything else seems as expected though. – miken32 May 12 '16 at 17:33
  • 1
    Possible duplicate of [How to implement a bitmask in php?](http://stackoverflow.com/questions/11880360/how-to-implement-a-bitmask-in-php) – miken32 May 12 '16 at 17:35
  • 1
    Numbers starting with `0` are considered octal (See [link](http://php.net/manual/en/language.types.integer.php)). There is no `9` in that system (or `8`, for completeness sake), hence it's invalid. Prior to PHP7 this was silently ignored and treated as zero. See the red warning in the link. Your `GLOB_CUSTOM` is effectively `0`. – ccKep May 12 '16 at 17:35

1 Answers1

2

In test one why is GLOB_CUSTOM showing as set ?

while GLOB_CUSTOM is defined as 123 and $flags is set to 168. The masking result in

$flags & GLOB_CUSTOM -> 40

and because you just test if($flags & GLOB_CUSTOM) and not if($flags & GLOB_CUSTOM === GLOB_CUSTOM) your functions shows the wrong result.

you should better use a switch case.

i think this solves also the question number 2

What does the value of the var_dump($flags) represent(where did that number come from)?

if you take the binary representation of this number, you'll see all the defined bits of your flags.

Raphael Müller
  • 2,180
  • 2
  • 15
  • 20