Reputation: 4242
Could someone clarify how to check for flags within a user defined function.
These constants are predefined glob flags.
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.
GLOB_CUSTOM
showing as set ? GLOB_BRACE
,GLOB_MARK
,GLOB_NOSORT
and GLOB_NOCHECK
showing as set ? 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);
Upvotes: 2
Views: 1096
Reputation: 2200
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.
Upvotes: 2