0
0
PHPprogramming~15 mins

Spaceship operator in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Spaceship Operator in PHP
📖 Scenario: You are building a simple ranking system for a game. You want to compare players' scores to see who is ahead, behind, or tied.
🎯 Goal: Learn how to use the PHP spaceship operator to compare two numbers and understand its output.
📋 What You'll Learn
Create two variables with exact scores
Create a variable to hold the comparison result using the spaceship operator
Print the comparison result
💡 Why This Matters
🌍 Real World
Comparing scores or values quickly is common in games, sorting, and decision-making.
💼 Career
Understanding comparison operators helps in writing clean and efficient code for sorting and conditional logic.
Progress0 / 4 steps
1
Create two score variables
Create two variables called $score1 and $score2 with values 85 and 90 respectively.
PHP
Need a hint?

Use the assignment operator = to set the values.

2
Create a comparison variable
Create a variable called $comparison that uses the spaceship operator <=> to compare $score1 and $score2.
PHP
Need a hint?

The spaceship operator returns -1, 0, or 1 depending on the comparison.

3
Add a condition to explain the result
Use an if statement to print 'Score1 is less' if $comparison is -1, 'Scores are equal' if 0, and 'Score1 is greater' if 1.
PHP
Need a hint?

Use strict comparison === to check the value of $comparison.

4
Print the final comparison result
Run the program to print the correct comparison message based on the spaceship operator result.
PHP
Need a hint?

The output should tell you which score is smaller or if they are equal.