Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Equality with type conversion (==)

Test whether two values are equal under PHP’s conversion rules. A numeric string can match a number despite leading zeros.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

$left == $right

Test whether two values are equal under PHP’s conversion rules. A numeric string can match a number despite leading zeros.

PHP compares a number and a numeric string by their numeric values. Leading zeros in a numeric string therefore cannot distinguish identifiers. Compare strings strictly when those zeros matter.

In PHP 8, comparing a number with a nonnumeric string converts the number to a string. Thus 4 and "4B" differ. When one operand is a boolean the other converts to boolean, making false and 0 loosely equal.

  • 4 == "04" → true
  • 4 == "4B" → false
  • false == 0 → true

Examples

<?php
echo json_encode(4 == "04"), PHP_EOL;
echo json_encode(4 == "4B"), PHP_EOL;
echo json_encode(false == 0), PHP_EOL;

Result

true
false
true

Sources