Booleans and Comparison Operators - Images: Japanese
Hey everyone!
Last time we tackled multidimensional arrays — nice work getting through that one. This time we're going to look at boolean values (true/false values) and comparison operators. Boolean values are also called booleans, so keep that term in mind — you'll see it come up all the time in textbooks and on the job.
Let's start with boolean values — the ones that are always black or white, never in between. A boolean value represents either 'true' or 'false', and that's it.
Here's how you write them:
<?php $bl[] = true; $bl[] = false; var_dump($bl);
Run that and you get:
array(2) {
[0]=>
bool(true)
[1]=>
bool(false)
}
'var_dump()' confirms they're stored as 'bool' — so they're being treated as booleans. Perfect.
One thing to watch out for: don't wrap 'true' or 'false' in quotes. If you do, they become strings, not booleans.
<?php $bl[] = 'true'; // Wrapping in quotes makes this a string, not a boolean. $bl[] = 'false'; var_dump($bl);
That covers how to write boolean values — pretty straightforward so far, right?
Now, the question is: when do you actually use them? A boolean just says "this is correct (true)" or "this is wrong (false)" — simple on its own, but enormously important in programming. They show up constantly. The best way to understand how they're used is to look at them alongside comparison operators, so let's jump into that now.
A comparison operator is an operator that compares two values — like checking which number is bigger, or whether two strings are the same — and returns a boolean based on the result.
You've probably seen these in math: <, <=, >, >= — those are exactly what comparison operators are.
Let's try a quick example. Inside var_dump(), we'll compare the number 1 and the number 2. Since 2 is bigger, 1 < 2 should come back as true. Let's verify:
<?php var_dump(1 < 2);
Run it:
bool(true)
true came back as expected. Now let's try 1 > 2:
<?php var_dump(1 > 2);
Result:
bool(false)
1 > 2 is incorrect, so false comes back. That's the basic idea — looking good?
Programming also gives you comparison operators that don't exist in math. Here's the full list:
| == | Returns 'true' if both operands are equal. |
| === | Returns 'true' if both operands are strictly equal. |
| != | Returns 'true' if both operands are not equal. |
| !== | Returns 'true' if both operands are strictly not equal. |
| > | Returns 'true' if the left operand is greater than the right operand. |
| >= | Returns 'true' if the left operand is greater than or equal to the right operand. |
| < | Returns 'true' if the left operand is less than the right operand. |
| <= | Returns 'true' if the left operand is less than or equal to the right operand. |
The word operand appeared in the table. We touched on it briefly in an earlier article, but let's review it here.
An operand refers to any value or variable that is the target of an operation. It's also called a 被演算子 (hi-enenzan-shi) in Japanese, which literally means "the thing being operated on." In 1 < 2, both 1 and 2 are operands.
Let's go through the operators one by one. First, === — the strict equality check. Writing 1 === 1 returns true because both sides are equal. Nothing tricky there. It also works with strings: "hoge" === "hoge" returns true because both are the string hoge.
Next is '!==', the strict inequality check. '1 !== 2' returns 'true' because the operands are not equal. '1 !== 1' returns 'false' because they are equal. Strings work here too: '"hoge" != "hoge"' returns 'false' because both sides are the same string 'hoge'.
In most languages other than PHP, the operator for checking equality is == (two equals signs), and != is used for inequality.
PHP supports == and != as well, but there's a catch: when you use == or !=, PHP performs automatic type conversion behind the scenes, without you asking for it.
Type conversion means something like a string '1' getting silently converted to the number '1' before the comparison happens. Here's what that looks like:
<?php
var_dump('1' == 1); // This returns 'true'.
In programming, you generally want comparisons to be strict and predictable — your program's logic is built on the assumption that operations behave exactly as written. For that reason, it's common practice in PHP to use === and !== when comparing operands. JavaScript also recommends === and !==.
That said, when receiving values from a database or another language, numbers or booleans can sometimes arrive as strings. In those cases, using == or != is perfectly fine, so learn to choose the right one depending on the situation.
Next up are > and >=. These are the "greater than" operators you learned in math class. The right operand must be smaller than, or equal to, the left operand for these to return true.
2 > 1→true1 > 1→false0 > 1→false1 >= 1→true
'<' and '<=' are just the mirror image of the above, so those should be easy enough.
One more thing worth knowing: PHP can compare strings in lexicographic (dictionary) order. This is sometimes used when building search-like functionality, so keep it in the back of your mind.
Here's a breakdown of how comparison operators behave depending on the types of operands involved:
| 1st Operand | 2nd Operand | Comparison Method |
|---|---|---|
| Number | Number | Numeric comparison |
| Numeric string | Numeric string | Numeric comparison |
| Numeric string | Number | Numeric comparison |
| Regular string | Number | Numeric comparison |
| Numeric string | Regular string | Lexicographic comparison |
| Regular string | Regular string | Lexicographic comparison |
A few things to watch out for in the table above. First, PHP treats two numeric strings as numbers when comparing them — not as dictionary strings. So be careful:
<?php
var_dump('2' < '1'); // This is processed as '2 < 1' (numeric comparison).
Next, comparing a regular string with a number also triggers numeric comparison. Take a look:
<?php
var_dump('1time' < 2); // This returns 'true'.
Comparing the string 1time with the number 2 using < returns true — which seems strange. What's happening behind the scenes is: "if the string starts with a number, extract that number; if it contains no number, treat it as 0." So '1time' becomes '1', and '1 < 2' is true.
As you can see, PHP's comparison operators can behave in some pretty unexpected ways. It's generally safer to avoid comparing strings with numbers directly.
As for lexicographic ordering — it works as the name implies, ordering things like entries in a dictionary. However, lexicographic ordering of Japanese text on computers often doesn't produce the expected result, so it's generally better to avoid relying on it for Japanese strings.
You can use variables with any of the comparison operators, by the way:
<?php $op1 = 1; $op2 = 2; var_dump($op1 < $op2); // Outputs 'true'.
The variable values are resolved before the comparison happens, so it works exactly as you'd expect. Good to know!
That wraps up boolean values and comparison operators. You might be wondering "okay, but when would I actually use these?" — the next article covers if statements, and that's where everything comes together. See you there!
Until next time!
This article was written by Sakurama.
Author's beloved small mammal |
桜舞 春人 Sakurama HarutoA Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him. |
If you find any errors or copyright issues, please contact us.