PHP - Concatenate Strings

0
5K

String Concatenation

To concatenate, or combine, two strings you can use the . operator:

ExampleGet your own PHP Server

$x = "Hello";
$y = "World";
$z = $x . $y;
echo $z;
Try it Yourself »

The result of the example above is HelloWorld, without a space between the two words.

You can add a space character like this:

Example

$x = "Hello";
$y = "World";
$z = $x . " " . $y;
echo $z;
Try it Yourself »

An easier and better way is by using the power of double quotes.

By surrounding the two variables in double quotes with a white space between them, the white space will also be present in the result:

Example

$x = "Hello";
$y = "World";
$z = "$x $y";
echo $z;
Try it Yourself »

Complete PHP String Reference

For a complete reference of all string functions, go to our complete PHP String Reference.

 
 

 

 

Search
Categories
Read More
Other
PHP - Modify Strings
PHP has a set of built-in functions that you can use to modify strings. Upper Case...
By PHP Tutorial 9 months ago 0 5K
Other
PHP echo and print Statements
With PHP, there are two basic ways to get output: echo and print. In this...
By PHP Tutorial 9 months ago 0 5K
Other
PHP Global Variables - Superglobals
Superglobals were introduced in PHP 4.1.0, and are built-in variables that are always available...
By PHP Tutorial 9 months ago 0 6K
Other
PHP Indexed Arrays
PHP Indexed Arrays In indexed arrays each item has an index number. By default, the first item...
By PHP Tutorial 9 months ago 0 3K
Other
PHP Sorting Arrays
The elements in an array can be sorted in alphabetical or numerical order, descending or...
By PHP Tutorial 9 months ago 0 5K