Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Inequality with type conversion (!=)

The condition is true when the values compare unequal after loose comparison. A difference in type alone cannot identify a changed product code.

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

Syntax

$left != $right

The condition is true when the values compare unequal after loose comparison. A difference in type alone cannot identify a changed product code.

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" → false
  • 4 != "4B" → true
  • false != 0 → false

Examples

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

Result

false
true
false

Sources