Comparison Operators
Part of the Fundamentals section of Coddy's PHP journey — lesson 23 of 71.
Comparison operators let you compare two values and get a boolean result: true or false. These operators are essential for making decisions in your code.
PHP provides several comparison operators for checking relationships between values:
| Operator | Meaning | Example | Result |
< | Less than | 5 < 10 | true |
> | Greater than | 5 > 10 | false |
<= | Less than or equal | 5 <= 5 | true |
>= | Greater than or equal | 10 >= 5 | true |
<?php
$age = 18;
$isAdult = $age >= 18; // true
$isMinor = $age < 18; // false
$score = 75;
$passed = $score >= 60; // true
?>To see the result of a comparison, you can use var_dump() which displays both the type and value. Note that var_dump() automatically adds a new line after each call, so you don't need to add one manually:
<?php
var_dump(10 > 5); // Outputs: bool(true)
var_dump(3 >= 7); // Outputs: bool(false)
?>Each var_dump() call prints its result on its own line automatically, so calling it multiple times will produce neatly separated output with no extra code needed.
These operators work with numbers and strings alike. In the next lesson, you'll learn about equality operators for checking if values are equal or identical.
Challenge
EasyRead two numbers from input: a score and a passingMark.
Use comparison operators to evaluate the following conditions and print the result of each using var_dump(), each on a new line:
- Check if
scoreis greater thanpassingMark - Check if
scoreis less thanpassingMark - Check if
scoreis greater than or equal topassingMark - Check if
scoreis less than or equal topassingMark
Example:
If the inputs are 75 and 60, the output should be:
bool(true)
bool(false)
bool(true)
bool(false)Explanation:
- 75 > 60 is true
- 75 < 60 is false
- 75 >= 60 is true
- 75 <= 60 is false
Cheat sheet
Comparison operators compare two values and return true or false.
| Operator | Meaning | Example | Result |
< | Less than | 5 < 10 | true |
> | Greater than | 5 > 10 | false |
<= | Less than or equal | 5 <= 5 | true |
>= | Greater than or equal | 10 >= 5 | true |
Use var_dump() to display the type and value of a comparison result:
<?php
var_dump(10 > 5); // Outputs: bool(true)
var_dump(3 >= 7); // Outputs: bool(false)
?>Try it yourself
<?php
// Read input
$score = intval(fgets(STDIN));
$passingMark = intval(fgets(STDIN));
// TODO: Write your code below
// Use comparison operators and var_dump() to check:
// 1. If score is greater than passingMark
// 2. If score is less than passingMark
// 3. If score is greater than or equal to passingMark
// 4. If score is less than or equal to passingMark
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types5Conditional Logic
If StatementIf - ElseThe Ternary OperatorNull Coalescing OperatorSwitch StatementRecap - Making Decisions3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators