📖 Scenario: You are working on a simple PHP script that compares values to check if they are equal or identical. This is useful when you want to understand how PHP treats different types of comparisons.
🎯 Goal: Build a PHP script that demonstrates the difference between loose equality (==) and strict equality (===) operators by comparing different values.
📋 What You'll Learn
Create variables with specific values
Create a variable to hold a comparison result
Use both loose and strict comparison operators
Print the comparison results
💡 Why This Matters
🌍 Real World
Understanding how PHP compares values helps avoid bugs when checking user input, form data, or database values.
💼 Career
Many PHP jobs require careful handling of data types and comparisons to ensure correct program logic and security.
Progress0 / 4 steps
1
Create variables with specific values
Create two variables called $a and $b. Set $a to the integer 5 and $b to the string '5'.
PHP
Hint
Use $a = 5; and $b = '5'; to create the variables.
2
Create a variable to hold a comparison result
Create a variable called $looseEqual and set it to the result of comparing $a and $b using the loose equality operator ==.
PHP
Hint
Use $looseEqual = ($a == $b); to compare loosely.
3
Use strict equality operator for comparison
Create a variable called $strictEqual and set it to the result of comparing $a and $b using the strict equality operator ===.
PHP
Hint
Use $strictEqual = ($a === $b); to compare strictly.
4
Print the comparison results
Print the values of $looseEqual and $strictEqual using var_dump() to show the comparison results.
PHP
Hint
Use var_dump($looseEqual); and var_dump($strictEqual); to print the results.
Practice
(1/5)
1. Which PHP comparison operator checks both value and type strictly?
easy
A. ===
B. ==
C. !=
D. <>
Solution
Step 1: Understand loose vs strict comparison
Loose comparison (==) checks value after type conversion, strict (===) checks value and type exactly.
Step 2: Identify operator that checks type and value
The operator === compares both type and value strictly without conversion.
Final Answer:
=== -> Option A
Quick Check:
Strict comparison = === [OK]
Hint: Use === to compare type and value exactly [OK]
Common Mistakes:
Confusing == with ===
Thinking != checks type
Using <> instead of !=
2. Which of the following is the correct syntax for a strict inequality check in PHP?
easy
A. !=
B. !==
C. <>
D. !===
Solution
Step 1: Recall PHP strict inequality operator
Strict inequality operator in PHP is !== which checks both type and value are not equal.
Step 2: Identify correct syntax
!== is valid syntax; !=== is invalid and others are loose inequality.
The condition uses = which assigns '5' to $x instead of comparing.
Step 2: Understand effect of assignment in condition
Assignment returns the assigned value, which is truthy, so 'Equal' always prints regardless of original $x.
Final Answer:
Assignment (=) used instead of comparison (==) -> Option C
Quick Check:
Use == for comparison, not = [OK]
Hint: Use == for comparison, not = assignment [OK]
Common Mistakes:
Confusing = with == in conditions
Thinking strict comparison is error here
Ignoring that assignment returns value
5. You want to filter an array to keep only elements that are exactly integer 0, excluding string '0'. Which comparison operator should you use in PHP?
hard
A. ==
B. !==
C. !=
D. ===
Solution
Step 1: Understand filtering requirement
We want to keep only elements that are integer 0, not string '0'. So type matters.
Step 2: Choose operator that checks type and value
Strict equality (===) checks both value and type, so it excludes string '0'.
Final Answer:
=== -> Option D
Quick Check:
Strict equality === filters exact type and value [OK]
Hint: Use === to match exact type and value [OK]
Common Mistakes:
Using == which allows type conversion
Using != or !== which exclude 0
Ignoring type difference in filtering
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.