String concatenation in PHP

This can be done many ways in PHP. I’m here going to show my two preferred ways:

My personal favorite:

echo string1, string2, string3

Example:

$score = 5;
echo "You scored ", $score, " points!";

Will print:

You scored 5 points!

Using print and dot syntax:

print string1.string2.string3

Example:

$score = 7;
print "You scored ".$score." points!";

Will print:

You scored 7 points!

I try to use the first syntax as much as possible because I believe that it is easier to read in code. Trying to keep it simple

String concatenation in PHP
Tagged on:
Niklas Tech Blog
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.