Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The < (less than) operator is a relational comparison operator that evaluates whether the value of the left operand is strictly smaller than the value of the right operand. It returns a boolean true if the condition is met, and false otherwise.
$result = $operand1 < $operand2;

Evaluation Mechanics and Type Coercion

Because PHP is a dynamically typed language, the < operator performs implicit type coercion (type juggling) when comparing operands of different data types. The engine applies specific rules based on the types being compared:
  • Numeric Comparison: When both operands are integers or floats, PHP performs a standard mathematical comparison.
  • String Comparison: If both strings are valid numeric strings (e.g., "12" and "9"), PHP casts them to numbers and performs a mathematical comparison. If one or both strings are non-numeric, PHP performs a byte-by-byte lexicographical comparison based on the ASCII values of the characters.
  • Mixed Type (Number and String):
    • PHP 8.0+: If the string is a valid numeric string (e.g., "123"), it is cast to a number and compared mathematically. If it is a non-numeric string (e.g., "apple"), the number is cast to a string and compared lexicographically.
    • PHP 7.x and older: The string is implicitly cast to a number. Non-numeric strings evaluate to 0, and the comparison is performed mathematically.
  • Boolean Comparison: When comparing a boolean with another data type, the other operand is implicitly cast to a boolean. The comparison is then performed strictly as a boolean comparison, where false is considered smaller than true. They are never cast to integers for this evaluation.
  • Array Comparison: Arrays are compared primarily by their size (count). An array with fewer elements is considered strictly less than an array with more elements. If the sizes are identical, PHP compares the elements sequentially only if both arrays have the exact same keys. If the arrays have the same number of elements but different keys, they are considered incomparable, and the < operator evaluates to false.

Syntax and Behavior Visualization

// Standard numeric comparison
var_dump(5 < 10);         // bool(true)
var_dump(10 < 10);        // bool(false) - strictly less than, not equal
var_dump(10.5 < 10.6);    // bool(true)

// String comparison (Lexicographical vs. Numeric Strings)
var_dump("apple" < "banana"); // bool(true) - lexicographical: 'a' (ASCII 97) < 'b' (ASCII 98)
var_dump("Z" < "a");          // bool(true) - lexicographical: 'Z' (ASCII 90) < 'a' (ASCII 97)
var_dump("12" < "9");         // bool(false) - numeric strings: cast to numbers and compared mathematically (12 < 9)

// Mixed type comparison (PHP 8.0+ behavior)
var_dump(5 < "10");       // bool(true) - "10" is recognized as numeric, compared as 5 < 10
var_dump(42 < "apple");   // bool(true) - 42 is cast to "42", compared lexicographically to "apple"

// Boolean comparison
var_dump(false < true);   // bool(true) - false is strictly smaller than true
var_dump(-5 < false);     // bool(false) - -5 is cast to boolean true; true < false evaluates to false

// Array comparison
$array1 = [1, 2];
$array2 = [1, 2, 3];
var_dump($array1 < $array2); // bool(true) - $array1 has fewer elements

$array3 = ["a" => 1, "b" => 2];
$array4 = ["c" => 1, "d" => 2];
var_dump($array3 < $array4); // bool(false) - identical size but different keys makes them incomparable
Master PHP with Deep Grasping Methodology!Learn More