prepend a string to the beginning of another string in php

One way to prepend a string to the beginning of another string in PHP is to use the concatenation operator (.) in the following way:

main.php
$prefix = 'Hello, ';
$fullName = $prefix . 'John Doe';
echo $fullName; // Output: Hello, John Doe
98 chars
4 lines

Alternatively, for a more concise solution, you can also use the string concatenation assignment operator (.=) like so:

main.php
$prefix = 'Hello, ';
$fullName = 'John Doe';
$fullName = $prefix . $fullName; // Or $fullName = $prefix .= $fullName;
echo $fullName; // Output: Hello, John Doe
161 chars
5 lines

In both cases, the output will be the same: Hello, John Doe.

gistlibby LogSnag