Strings

Strings are lines of symbols or characters, such as words or phrases. They can be stored in variables, sent directly to the browser using the echo statement, or used in various functions. Strings do not have a size/length limit.

As we have already learned, strings begin and end with either single quotes or double quotes. Variables are understood to be variables when they are placed inside double-quoted strings, but variables are nothing more than additional text when placed inside single-quoted strings.

String Escape Characters

What happens when there are single quotes inside a single-quoted string, double quotes inside a double-quoted string, or you actually want the dollar sign to be a part of your string instead of referencing a variable? The backslash is used to escape characters such as these in double-quoted strings. The only character inside a single-quoted string that will require escaping is a single quote (apostrophe).

Character Meaning
\' Escapes A Single Quote In A Single-Quoted String
\" Escapes A Double Quote In A Double-Quoted String
\$ Escapes A Dollar Sign In A Double-Quoted String
\\ Escapes A Backslash In A Double-Quoted String
\n Creates A New Line, Not In HTML, But In Files, Etc.

Examples:

<?php
  $string = 'This string\'s extra apostrophe is escaped.';
  $string = "This \"string\" contains escaped double quotes.";
  $string = "This \$0.00 string contains an escaped dollar sign.";
?>

Strings are rather straight-forward once you get the hang of them, so let's move on to some methods of manipulating strings.

String Concatenation (The "Dot" Operator)

The word "concatenate" means "to link together, or to unite in a series or a chain". It is possible to link together a series of one or more strings using the concatenation or "dot" operator, as it is informally called. Let's look at an example first.

<?php
  $variable1 = "abcdefghijklm";
  $variable2 = "nopqrstuvwxyz";
  $variable3 = $variable1 . $variable2;
  echo $variable3;
  echo $variable1 . $variable2;
?>

What we see in our example are two variables containing strings, and a third variable using the concatenation operator to combine those two variables and store the contents of them both in a new variable. When the contents of the third variable are sent to the browser, the result will be "abcdefghijklmnopqrstuvwxyz", because the concatenation operator added what was on the right (variable #2) to the end of what was on the left (variable #1).

String concatenation can be performed directly from within an echo statement as well. And, in both variables and in the echo statement, strings and variables can be interchangeably concatenated.

<?php
  $variable1 = "abcdefghijklm";
  $variable2 = "nopqrstuvwxyz";
  echo "The letters of the alphabet are: " . $variable1 . $variable2;
?>

Can you see what the result will be?

The letters of the alphabet are: abcdefghijklmnopqrstuvwxyz

String Concatenation Assignment

The assignment operator .= is used as a shortcut to concatenate two strings without assigning a new variable. It takes an existing variable and adds a new string onto the end (right side) of that variable's existing string.

<?php
  $variable = "01234";
  $variable .= "56789";
  echo $variable;
?>

The result is:

0123456789

Next, we will take a look at a number of functions that can manipulate strings in various ways.