Menu
Coddy logo textTech

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:

OperatorMeaningExampleResult
<Less than5 < 10true
>Greater than5 > 10false
<=Less than or equal5 <= 5true
>=Greater than or equal10 >= 5true
<?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 icon

Challenge

Easy

Read 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:

  1. Check if score is greater than passingMark
  2. Check if score is less than passingMark
  3. Check if score is greater than or equal to passingMark
  4. Check if score is less than or equal to passingMark

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.

OperatorMeaningExampleResult
<Less than5 < 10true
>Greater than5 > 10false
<=Less than or equal5 <= 5true
>=Greater than or equal10 >= 5true

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

?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals