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 . (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.
$result = $expression1 . $expression2;

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".
$str1 = "Value: " . 100;        // Evaluates to "Value: 100"
$str2 = "Status: " . true;      // Evaluates to "Status: 1"
$str3 = "Data: " . null;        // Evaluates to "Data: "

Associativity and Precedence

The . operator is left-associative, meaning chained concatenations are evaluated strictly from left to right.
$result = "A" . "B" . "C"; 
// Evaluated as: ("A" . "B") . "C"
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 -.
// PHP 7.x evaluation:
echo "Sum: " . 1 + 2; 
// Evaluates as: ("Sum: " . 1) + 2 
// -> "Sum: 1" + 2 
// -> 0 + 2 
// Outputs: 2 (Emits a Warning/Notice because the non-numeric string "Sum: 1" is cast to 0)

// PHP 8.0+ evaluation:
echo "Sum: " . 1 + 2; 
// Evaluates as: "Sum: " . (1 + 2) 
// -> "Sum: " . 3 
// Outputs: "Sum: 3"
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.
$variable = "Initial";
$variable .= "Suffix"; 
// Syntactic sugar for: $variable = $variable . "Suffix";
Master PHP with Deep Grasping Methodology!Learn More