Computer >> Computer tutorials >  >> Programming >> PHP

PHP String Operators


Introduction

There are two operators for working with string data types. The '.' (dot) operator is PHP's concatenation operator. Two string operands are joined together (characters of right hand string appended to left hand string) and returns a new string. PHP also has .= operator which can be termed as concatenation assignment operator string on left is updated by appending characters of right hand operand.

Syntax

$newstring = $first . $second // concatenation operator
$leftstring .= $rightstring

Following uses concatenation operator

Example

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

Output

Following result will be displayed

Hello PHP

Following example uses concatenation assignment operator. Twostring operands are concatenated returning updated contents of string on left side

Example

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

Output

Following result will be displayed

Hello PHP