• PHP Video Tutorials

PHP – String Operators



There are two operators in PHP for working with string data types: concatenation operator (".") and the concatenation assignment operator (".="). Read this chapter to learn how these operators work in PHP.

Concatenation Operator in PHP

The dot operator (".") is PHP's concatenation operator. It joins two string operands (characters of right hand string appended to left hand string) and returns a new string.

$third = $first . $second;

Example

The following example shows how you can use the concatenation operator in PHP −

<?php
   $x="Hello";
   $y=" ";
   $z="PHP";
   $str=$x . $y . $z;
   echo $str;
?>

It will produce the following output

Hello PHP

Concatenation Assignment Operator in PHP

PHP also has the ".=" operator which can be termed as the concatenation assignment operator. It updates the string on its left by appending the characters of right hand operand.

$leftstring .= $rightstring;

Example

The following example uses the concatenation assignment operator. Two string operands are concatenated returning the updated contents of string on the left side −

<?php
   $x="Hello ";
   $y="PHP";
   $x .= $y;
   echo $x;
?>

It will produce the following output

Hello PHP
Advertisements