Menu
Coddy logo textTech

Equality & Identity

Part of the Fundamentals section of Coddy's PHP journey — lesson 24 of 71.

PHP provides two ways to check if values are equal: the equality operator (==) and the identity operator (===). Understanding the difference is crucial for writing reliable code.

The equality operator (==) compares values after converting them to the same type. This is called "loose comparison":

<?php
var_dump(5 == "5");    // bool(true) - string converted to number
var_dump(0 == false);  // bool(true) - both become 0
var_dump(1 == true);   // bool(true) - both become 1
?>

The identity operator (===) compares both value and type. No conversion happens, so both must match exactly:

<?php
var_dump(5 === "5");   // bool(false) - int vs string
var_dump(5 === 5);     // bool(true) - same type and value
var_dump(0 === false); // bool(false) - int vs bool
?>

For "not equal" comparisons, use != (loose) or !== (strict):

<?php
var_dump(5 != "5");    // bool(false) - values are equal
var_dump(5 !== "5");   // bool(true) - types differ
?>

As a best practice, prefer === and !== to avoid unexpected type conversion surprises.

challenge icon

Challenge

Easy

Read two values from input: a number (as a string) and an integer.

Use the equality and identity operators to evaluate the following comparisons and print the result of each using var_dump(), each on a new line:

  1. Check if the string value equals the integer using loose comparison (==)
  2. Check if the string value equals the integer using strict comparison (===)
  3. Check if the string value is not equal to the integer using loose comparison (!=)
  4. Check if the string value is not equal to the integer using strict comparison (!==)

Note: Keep the first value as a string (do not cast it). Cast the second value to an integer.

Example:

If the inputs are 42 and 42, the output should be:

bool(true)
bool(false)
bool(false)
bool(true)

Explanation:

  • "42" == 42 is true (values are equal after type conversion)
  • "42" === 42 is false (string vs int - different types)
  • "42" != 42 is false (values are equal)
  • "42" !== 42 is true (types are different)

Cheat sheet

PHP has two ways to compare values:

Equality operator (==) - loose comparison that converts values to the same type before comparing:

<?php
var_dump(5 == "5");    // bool(true) - string converted to number
var_dump(0 == false);  // bool(true) - both become 0
var_dump(1 == true);   // bool(true) - both become 1
?>

Identity operator (===) - strict comparison that checks both value and type:

<?php
var_dump(5 === "5");   // bool(false) - int vs string
var_dump(5 === 5);     // bool(true) - same type and value
var_dump(0 === false); // bool(false) - int vs bool
?>

Not equal operators:

<?php
var_dump(5 != "5");    // bool(false) - values are equal (loose)
var_dump(5 !== "5");   // bool(true) - types differ (strict)
?>

Best practice: Use === and !== to avoid unexpected type conversion.

Try it yourself

<?php
// Read input
$stringValue = trim(fgets(STDIN));
$intValue = intval(trim(fgets(STDIN)));

// TODO: Write your code below
// Use var_dump() to print the result of each comparison on a new line:
// 1. Loose equality (==)
// 2. Strict equality (===)
// 3. Loose inequality (!=)
// 4. Strict inequality (!==)

?>
quiz iconTest yourself

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

All lessons in Fundamentals