Skip to main content
The . (dot) operator in PHP is the string concatenation operator. It evaluates two operands, performs implicit type coercion if necessary, and returns a single new string consisting of the left operand’s value immediately followed by the right operand’s value.

Mechanics and Type Coercion

When the Zend Engine evaluates the . operator, it expects both operands to be of type string. If an operand is not a string, PHP applies standard type juggling rules to cast the operand before concatenation:
  • Integers and Floats: Converted to their literal string representations (e.g., 42 becomes "42", 3.14 becomes "3.14").
  • Booleans: true is cast to "1", and false is cast to "" (an empty string).
  • Null: Cast to "" (an empty string).
  • Objects: The object must implement the __toString() magic method. If it does not, PHP throws a fatal error (Uncaught Error: Object of class X could not be converted to string).
  • Arrays: Triggers a PHP Warning: Array to string conversion and evaluates to the literal string "Array".

Associativity and Precedence

The . operator is left-associative, meaning chained concatenations are evaluated strictly from left to right.
Precedence Shift (PHP 8.0+): Prior to PHP 8.0, the . operator had the exact same precedence as the addition (+) and subtraction (-) operators, which often led to unexpected evaluations when mixed. As of PHP 8.0, the . operator has a lower precedence than + and -.
Note: Despite the PHP 8.0 precedence fix, wrapping mathematical operations in parentheses when concatenating is the standard syntactic convention to ensure predictable evaluation.

Concatenation Assignment Operator (.=)

PHP provides a compound assignment operator for concatenation. The .= operator appends the right operand to the existing string value of the left operand, mutating the left operand in place.
Tired of Poor PHP Skills? Fix That With Deep Grasping!Learn More