String Operators

Learn how to concatenate two strings using string operators in this lesson.

We'll cover the following

There are only two string operators:

  • Concatenation (.):
  • Concatenation Assignment (.=)

Concatenation

The most important operation in strings is concatenation. It means joining one string to another. For instance, the concatenation of water and bottle will result in a string water bottle.

Run the code below to see how this is done:

Press + to interact
$a = "water";
$b = " bottle";
$c = $a . $b; # $c => water bottle
print $c;

Concatenation assignment

Concatenation assignment is a slight variation of concatenation where you concatenate (add) one string at the end of another without the need for a third destination string. The difference between concatenation and concatenation assignment is similar to + and +=.

Run the code below to see how this works:

Press + to interact
$a = "Hello";
$a .= " World"; # $a => Hello World
print $a;