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 or equal to) operator is a relational comparison operator that evaluates two operands and returns a boolean true if the value of the left operand is mathematically, lexicographically, or structurally less than or equal to the value of the right operand. Otherwise, it returns false.

Syntax

$result = $expression1 <= $expression2;

Evaluation Mechanics and Type Coercion

Because PHP is a dynamically typed language, the <= operator performs implicit type coercion (type juggling) when evaluating operands of differing data types. It is a non-strict operator, meaning it evaluates value equivalence rather than strict type identity. The evaluation engine applies the following rules based on operand types:
  1. Numeric vs. Numeric: Performs a standard mathematical comparison of integers or floats.
  2. String vs. String: Performs a lexicographical comparison. The engine evaluates the strings byte-by-byte based on their ASCII values.
  3. Numeric String vs. Number: If a string contains a valid numeric value, PHP casts the string to an integer or float before performing a mathematical comparison.
  4. Non-Numeric String vs. Number (PHP 8.0+): The engine casts the number to a string and performs a lexicographical string comparison. (Note: In PHP < 8.0, the string was cast to 0 and compared mathematically).
  5. Boolean vs. Other Types: The non-boolean operand is cast to a boolean, and a boolean comparison is performed. In PHP, false is considered less than true.
  6. Array vs. Array: Evaluates based on the number of elements first. An array with fewer elements is considered less than an array with more elements. If the element counts are equal, the engine performs an element-by-element comparison.

Code Visualization

// Standard numeric evaluation
var_dump(5 <= 10);    // bool(true)
var_dump(10 <= 10);   // bool(true)
var_dump(15 <= 10);   // bool(false)

// Lexicographical string evaluation
var_dump("apple" <= "banana"); // bool(true)
var_dump("Z" <= "a");          // bool(true) (ASCII: 'Z' is 90, 'a' is 97)

// Type coercion: Numeric string and integer
var_dump("10" <= 10); // bool(true) (String "10" is cast to int 10)
var_dump(5 <= "10");  // bool(true)

// Type coercion: Boolean and integer
var_dump(false <= 0); // bool(true) (0 is cast to false, evaluates as false <= false)
var_dump(true <= 2);  // bool(true) (2 is cast to true, evaluates as true <= true)

// PHP 8.0+ Mixed type evaluation (Number vs Non-numeric string)
var_dump(0 <= "foo"); // bool(true) (0 is cast to "0", lexicographically less than "foo")

// Array evaluation
var_dump([1, 2] <= [1, 2, 3]);    // bool(true) (Left array has fewer elements)
var_dump([1, 2, 3] <= [1, 2, 3]); // bool(true) (Equal element count and values)
var_dump([1, 5, 3] <= [1, 2, 3]); // bool(false) (Equal count, element-by-element fails at 5 <= 2)

Operator Precedence

The <= operator has lower precedence than arithmetic operators (e.g., +, -, *) and bitwise shift/NOT operators (<<, >>, ~). However, it has higher precedence than bitwise AND, XOR, and OR operators (&, ^, |), logical operators (e.g., &&, ||), and assignment operators (=).
// Arithmetic operations are evaluated before the <= comparison
$result = 5 + 5 <= 10 * 2; 
// Evaluates as: (5 + 5) <= (10 * 2) -> 10 <= 20 -> true

// <= is evaluated before bitwise AND (&)
$result = 10 & 5 <= 5;
// Evaluates as: 10 & (5 <= 5) -> 10 & true -> 10 & 1 -> 0
Master PHP with Deep Grasping Methodology!Learn More