This operator is used to concatenate strings.
EDIT
Well, more precisely, if the value is not a string, it must be converted to one. See Convert to String for more details.
Unfortunately, this is sometimes misused to make things harder to read. Well used here:
echo "This is the result of the function: " . myfunction();
Here we combine the output of the function. This is normal because we have no way to do this using the standard inline string syntax. A few ways to misuse:
echo "The result is: " . $result;
Here you have a variable called $result
, which we can embed in a string:
echo "The result is: $result";
Others find it difficult to misuse:
echo "The results are: " . $myarray['myvalue'] . " and " . $class->property;
This is a little tricky if you are not aware of the escape sequence {}
for inserting variables:
echo "The results are: {$myarray['myvalue']} and {$class->property}";
Example:
$headers = 'From: webmaster@example.com ' . "\r\n" . 'Reply-To: webmaster@example.com ' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
This is a bit more complicated because if we do not use the concatenation operator, we can send a new line randomly, so this forces the lines to end with "\ r \ n". I find this a more unusual case due to email header restrictions.
Remember that these concatenation operators fail, making things a little harder to read, so use them only when necessary.
onteria_
source share