Laravel 10.22, weekly updates, and 🔥 tip

Laravel 10.22

A patch release yesterday and a few new features today brings us to Laravel 10.22. Here are the highlights:

  • Add enum support to the in and notin validation rules in #48247
  • Introduce requireEnv in #48261
  • Add ULID testing helpers in #48276

You may review the full branch diff on GitHub for a complete list of changes.

This version bump and update is automated for subscribers to a Shifty Plan. If you don't have one of those, be sure to bump your constraint and run composer update to get the latest features.

Weekly Journal

Last week I created a new Shift - HTML Converter. This was the result of recent Human Shifts where I had to manually perform the migration between the two packages.

I normally don't make Shifts for packages. However, the laravelcollective/html package gained popularity way back in Laravel 5.1. From internal stats, it's still one of the most popular packages. I use it for laravelshift.com. Since it is now abandoned, it will be a blocker when upgrading to Laravel 11. So makes sense for Shift to help.

Unfortunately I lost focus as the week went on after stirring up an actual hornets nest, as well as a figurative one on Twitter.

Long story short, my code snippet was confusing (which I'll revisit below). When initially brought to my attention, I thought they were talking about bitwise operators in general. Maybe they thought I was defending the specific code snippet. Then there was trolling. Then Aaron Francis got involved. Then ThePrimeagen. Then the whole thing felt like:

That's a part of Twitter I'll never understand. Over the years I've witnessed countless attacks on Taylor and Adam. As most of you, I stay on the sidelines. That's not so easy when it's directed right at you. It's no fun. It's demoralizing. It's alienating. No one comes out clean (no pun).

Anyway, bear with me while I revisit last week's tip.

🔥 Tip

To paraphrase Aaron Francis, bitwise operators are good, the example was bad. My $dirty |= true; was a single line from a larger example. To be concise, I also evaluated the right hand side to true. This made it inaccurate and caused confusion.

As noted in my original tip, a common use of bitwise operators are flags or state. My example was for state (tracking dirtiness). But, the example simply assigns true. So the state is always true. That doesn't really relay the power of bitwise operators.

The actual code used within Shift has an expression on the right hand side: $changes |= $count > 0;

This makes a big difference as the value is now dynamic - meaning the expression can also evaluate to false. So $changes is only toggled if $count was greater than zero.

Now with the actual code, let me address some other issues that came up on Twitter. First, why not just use a boolean?

The goal here is to demonstrate how bitwise operators can streamline boolean logic. This same logic, written traditionally might be:

$changes = $changes || $count > 0;

Or:

if ($count > 0) {
$changes = true;
}

To me, these have drawbacks. The first now has a compound expression. Which is more dense than the bitwise assignment. The second, is straightforward, but more lines of code. Inside of a loop (which the actual code is) that's noisy.

Without bitwise operators, I'd probably write the second example. But my intention was to demonstrate the power of bitwise operators, not necessarily their readability. Using a bitwise operator, I can write a little one-liner and not have to think about if-this-then-that or parse a compound expression.

Second issue from Twitter, bitwise operations on boolean values is redundant/pointless. On the contrary, it's a way to streamline code. Maybe you wouldn't typically use them with boolean values, but that doesn't mean you can't.

Technically bitwise operations can be performed on any simple numerical or boolean value (PHP emits an error for anything else). The value is converted to binary and then the operation is applied. So true is converted to 00000001, false to 00000000, 2 to 00000010, 8.0 to 00001000. You could even use ord('s') (ASCII code 115) to perform bitwise operations on strings (think cyphers).

In fairness, you could drop the logical comparison and simply write: $changes |= $count;. The result is more or less the same. But this presents a new question, why not just track the number of changes? For example, with a more common assignment like $changes += $count;. For me, using the logical comparison better relays the intent of this variable - tracking state (on/off).

In the end, it's easiest to think of using |= with boolean values as a way to create ||=. Which currently doesn't exist in PHP. If it did, I'd use it. But it doesn't. |= is how you might simulate it.

So, if you're still reading, thanks for sticking with me. Hopefully this better demonstrates the power of bitwise operators, and how they may indeed be used with boolean values to streamline boolean logic.