php

PHP String Concatenation

PHP String Concatenation
Joining one or more string value with other string value or variable is called string concatenation. It is a common task when we want to print any output in a more meaningful way for the user. There are several ways to join strings in PHP. Different types of string concatenation in PHP are shown in this tutorial by using multiple examples.

Example-1: Concatenation using dot (.) operator

String concatenation is normally done by using dot (.) operator in PHP. In the following example, two string variables are declared and dot (.) operator is used to combine these strings with other string and print.

$string1 = "Charles Babbage";
$string2 = "Computer";
//Join string variables with a string value by using dot (.)
echo $string1." is considered as a father of ". $string2;
?>

Output:

Example-2: Concatenation using echo and comma (,) 

You can easily combine and print string data without using dot (.) operator in PHP by using echo function. Three string variables are declared in the following example. Here, “,” is used to join string value with the variables in echo function. Each variable and string value are separated by comma (,) to combine. This type of joining can be done by using echo function only.

$name = "John";
$education = "MBA";
$profession = "Business man";
// Combine data using comma (,)
echo "$name"," is an ","$education.
";
echo "He is a ","$profession.";
?>

Output:

Example-3: Concatenation using printf() function

You can use printf() function also for concatenating string values in PHP.  There are many type specifiers are available in PHP to generate formatted output. Two string variables, $site and $type are declared in the following example. Printf() function uses '%s' as type specifier to read string data. Two string variables are added at the beginning and end of the string by adding two '%s' in the first argument and two string variables as other two arguments.

$site = "linuxhint.com";
$type = "blog site";
printf("%s is a very helpful %s",$site,$type);
?>

Output:

Example-4: Concatenation using shorthand operator (.=)

Dot (.) can be used as shorthand operator for concatenating string data. In the following example, $n variable is initialized by 0 and the current value of $n is combined with new value using for loop and “.=” operator. Finally print the value of $n.

$n = 0;
for($i=1; $i<10; $i++)

$n .= " $i" ;

echo $n;
?>

Output:

I hope you will be able to combine string data efficiently in your script if you exercise the above examples properly.

Gry How to Show FPS Counter in Linux Games
How to Show FPS Counter in Linux Games
Linux gaming got a major push when Valve announced Linux support for Steam client and their games in 2012. Since then, many AAA and indie games have m...
Gry How to download and Play Sid Meier's Civilization VI on Linux
How to download and Play Sid Meier's Civilization VI on Linux
Introduction to the game Civilization 6 is a modern take on the classic concept introduced in the series of the Age of Empires games. The idea was fai...
Gry How to Install and Play Doom on Linux
How to Install and Play Doom on Linux
Introduction to Doom The Doom Series originated in the 90s after the release of the original Doom. It was an instant hit and from that time onwards th...