In this chapter
We'll take a short, practical look at bitwise operators — the &, |, ~ and ^ you'll meet in settings like E_ALL & ~E_DEPRECATED and flags like JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE — and at the error-control operator @, which hides warnings, and why you should almost never use it.
The Problem in Real Life
Mia looks again at the php.ini line from Act 01: error_reporting = E_ALL & ~E_DEPRECATED. She'd guessed it meant "all errors, except deprecated ones". Marcus says she guessed right — but asks how & and ~ could possibly mean "except".
Then he scrolls through the freelancer's price-loading code. One line: $prices = @file_get_contents("prices.txt");. Last month someone renamed the file. The page showed no prices, no error, no warning — nothing in the log. It took a day to find.
"The first one you'll read now and then," says Marcus. "The second one, you'll delete wherever you find it."
How can & and ~ mean "everything except deprecated"?
Mia
Settings as Switches vs. Settings as Words
Many switches in one number
Some settings pack several on/off options into one integer.
Strange-looking settings
E_ALL & ~E_DEPRECATED and JSON_PRETTY_PRINT | … only make sense once you know the operators.
A silenced error
@ hid a missing file completely — no warning, no log entry, just empty prices.
Bitwise and Error-Control Operators
Bits: inside the computer, every int is stored as bits — binary digits, each 0 or 1. The number 6 is 110 in binary; 3 is 011. Bitwise operators work on those bits one position at a time. You won't do binary maths in everyday PHP, but bitwise operators have one very common use — flags — so you need to be able to read them:
- And, written &: keeps a bit only where both numbers have it.
- Or, written |: keeps a bit where either number has it.
- Not, written ~: flips every bit.
- Xor, written ^: keeps a bit where exactly one number has it.
- Shifts, written << and >>: move the bits left or right.
What bitwise operators do: the code below shows each operator on 6 (110) and 3 (011). 6 & 3 keeps only the bit both have: 010, which is 2. 6 | 3 keeps every bit either has: 111, which is 7. 6 ^ 3 keeps the bits only one has: 101, which is 5. Don't worry about memorising this — what matters is the idea that one number can hold many separate on/off switches, one per bit.
<?phpvar_dump(6 & 3); // int(2): 110 & 011 = 010var_dump(6 | 3); // int(7): 110 | 011 = 111var_dump(6 ^ 3); // int(5): 110 ^ 011 = 101var_dump(1 << 3); // int(8): 001 shifted left 3 = 1000var_dump(16 >> 2); // int(4): 10000 shifted right 2 = 100
Flags: that idea is called flags. Each option gets its own bit: option A is 1 (001), option B is 2 (010), option C is 4 (100). To switch on several options, combine them with |: A | B is 3 (011) — "A and B are on". To ask whether an option is on, use &: ($flags & B) !== 0 means "is B's bit set?". PHP's own functions use this everywhere. json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) means "pretty-print and keep letters like ë as they are". The | reads as "plus this option too".
<?phpconst CAN_BOOK = 1; // 001const CAN_CANCEL = 2; // 010const CAN_REFUND = 4; // 100$receptionist = CAN_BOOK | CAN_CANCEL; // 011: book and cancelvar_dump(($receptionist & CAN_CANCEL) !== 0); // bool(true)var_dump(($receptionist & CAN_REFUND) !== 0); // bool(false)
<?php$booking = ["customer" => "Zoë", "time" => "10:15"];echo json_encode($booking, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
Prints the booking as nicely indented JSON, with "Zoë" kept as it is (without the second flag it would be written as Zo\u00eb). JSON comes in Act 19.
Reading E_ALL & ~E_DEPRECATED: now the php.ini line makes sense. Every error level is one bit. E_ALL has every bit on. ~E_DEPRECATED flips the deprecated bit — so it's "every bit except deprecated". & keeps only the bits that are in both: all of E_ALL, minus the deprecated bit. So E_ALL & ~E_DEPRECATED really does mean "report everything except deprecation notices". The pattern & ~X always means "switch X off". (On Mia's development laptop, keep plain E_ALL — you want to see deprecations early.)
| You want to… | Write | Example |
|---|---|---|
| Switch on several options | A | B | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE |
| Check whether one is on | ($flags & A) !== 0 | ($permissions & CAN_CANCEL) !== 0 |
| Switch one off | $flags & ~A | E_ALL & ~E_DEPRECATED |
The error-control operator, @: put @ in front of an expression, and PHP hides any warning or notice it produces. @file_get_contents("prices.txt") returns false if the file is missing — silently. That's why the freelancer's bug took a day to find: the one message that would have explained it was thrown away. @ makes code slower, hides real problems, and makes debugging miserable. It doesn't stop fatal errors either. The right approach is to check first — for files, file_exists() — and handle the false result openly (Act 14 shows proper error handling). If you find @ in code you maintain, treat it as a bug report waiting to happen.
<?php$prices = @file_get_contents("prices.txt"); // missing file: no warning at allvar_dump($prices); // bool(false)// Better: check, and say what's wrongif (!file_exists("prices.txt")) {echo "Price list missing: prices.txt\n";}
Key Takeaway
Bitwise operators (&, |, ^, ~, <<, >>) work on the bits of ints. Their everyday use is flags: combine options with |, test one with ($flags & OPTION) !== 0, and switch one off with & ~OPTION — which is why E_ALL & ~E_DEPRECATED means "everything except deprecations". The @ operator hides warnings; don't use it — check and handle failures instead.
Why This Matters
Mia won't write binary maths for BookBay, but she'll read flags constantly: in php.ini, in json_encode(), in file functions and in libraries. Knowing that | means "plus this option" and & ~ means "minus this option" makes all of them readable.
And removing @ is one of the cheapest ways to make a codebase easier to debug. Every warning it hides is a clue somebody will need one day.
Mia deletes the @ and adds a proper check — and the very next day the log tells her exactly which file a test server is missing. Then Olivia asks for a small feature: customers may type a discount code, a note and a number of people, all optional. Mia's code fills in defaults with ?: — and a deliberate 0% discount turns into 10%. That's the next chapter.
