Skip to content
PHP Logo

Why should binary flags be connected with | instead of & when using them in PHP functions like glob()?

When using binary flags in PHP functions like glob(), they should be connected with the bitwise OR operator (|) instead of the bitwise AND operator (&). This is because binary flags represent individual bits that should be combined using OR to set multiple options simultaneously. Using AND would only select files that match all specified flags, which is not the intended behavior when using multiple flags.

php
// Correct way to use binary flags with glob()
$files = glob('/path/to/directory/*.txt', GLOB_BRACE | GLOB_NOCHECK | GLOB_MARK);