Bitmask Constant Arguments in PHP

Liam Hammett
Aug 23, 2018 · 4 min read

Note: This post has been moved to my personal website, check it out to read the most up-to-date version and keep up with my latest postings:

PHP has a handful of core functions that can accept boolean arguments in the form of constants that have a binary value.

These can be combined together in a single function argument, essentially passing multiple boolean flags in a very compact manner.

They work a bit differently to how most people implement options in their userland functions, so let’s take a look at how they work.

How does core PHP use them?

The core PHP language uses these a lot for manipulating the behaviour of a handful of functions. As of PHP 7.2 with just a couple of extensions, there are over 1800+ constants defined, a lot of which are used as function arguments.

One example of these in use is the new option in PHP 7.3 to make throw exceptions upon encountering an error.

Using the bitwise operator you can also pass multiple arguments at once, like this example from the docs.

Pretty nifty.

But how do they work?

Using these bitmask operators to achieve the same effect in userland functions is pretty simple — but it requires at least a rudimentary understanding of how bits and certain bitwise operations work in PHP.

Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation. […] To use binary notation precede the number with 0b.

We’ll keep this fairly straightforward by defining some binary integer literals as constants that each have a different bit set.

The gist of it is that each binary value will represent a value twice as high for each zero on the end of it. The zeroes between the and are completely optional, but can help line up the source code.

We want each of our constants to have a unique value.

Luckily, we only need to understand two bitwise operators to get a grasp on how to use these values for our goal.

“Bitwise Inclusive OR”

The (“bitwise inclusive or”) is not to be confused with the more commonly used (“logical or”) operator you may find in “if” statements. Instead, it returns the bits that were set in either one or the other value.

Since each of our original constants has a unique bit, we can combine any amount of these to get a new unique set of bits back, which we can use as a single value.

“Bitwise AND“

Similarly, the (“bitwise and”) operator is not to be confused with the more commonly used (“logical and”) operator found in “if” statements. It instead returns the bits that are set in both values.

A number can be a boolean?

It’s worth noting at this point that PHP has a concept called “type juggling”. In laymen’s terms, this means it will automatically attempt to cast one type of data to another if it needs to.

This can come in pretty handy when you understand what gets cast to another type and when.

For example, we know that the integer will act as if cast to a boolean, and any other integer will act as . Remember how these binary values we’re working with are actually integers?

Let’s put it all together!

We can now combine this knowledge to create an “if” statement that will only pass if a given bit out of many is present.

That’s pretty much exactly how other functions like can be interacted with, sweet!

Is it worth it?

Even if it may seem tempting to be able to pass any number of boolean values in a single argument, there are downsides to this approach to handling feature flags:

  • You can’t pass non-boolean values through the argument like you could with an associative array
  • There’s no standard way to document the arguments with docblocks
  • You lose general hints and most IDE support that you would get if you were passing a single boolean value as an argument
  • You could instead pass an associative array as the argument to be able to set non-boolean values (or even a class)
  • There’s strong reasons why you should avoid using boolean “feature flags” as arguments in general in the first place, and instead opt for making another function or method for the altered functionality

But now you know how to do it, if you ever wanted to.

It can be a bit verbose and difficult to read a long collection of these constant definitions, so here’s a helper function to make defining them simpler.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store