News & UpdatesProgrammingWeb programming StoreMy Projects
Links
Affiliates

PHP Tutorial – 05 – Strings

A string is a series of characters that can be stored in a variable. In PHP, strings are typically delimited by single quotes.

$a = 'Hello';

String concatenation

PHP has two string operators. The dot symbol is known as the concatenation operator (.) and combines two strings into one. It also has an accompanying assignment operator (.=), which appends the right-hand string to the left-hand string variable.

$b = $a . ' World'; // Hello World
$a .= ' World';     // Hello World

Delimiting strings

PHP strings can be delimited in four different ways. There are two single-line notations: double-quote (" ") and single-quote (' '). The difference between them is that variables are not parsed in single-quoted strings whereas they are parsed in double-quoted strings.

$c = 'World';
echo "Hello $c"; // "Hello World"
echo 'Hello $c'; // "Hello $b"

Single-quoted strings tend to be preferred unless parsing is desired, mainly because string parsing has a very small performance overhead. However, double-quoted strings are considered easier to read, which makes the choice more a matter of preference.

In addition to single-quoted and double-quoted strings, there are two multi-line notations: heredoc and nowdoc. These notations are mainly used to include larger blocks of text.

Heredoc strings

The heredoc syntax consists of the <<< operator followed by an identifier and a new line. The string is then included followed by a new line containing the identifier in order to close the string. Variables are parsed inside of a heredoc string, just as with double-quoted strings.

$s = <<<LABEL
Heredoc (with parsing)
LABEL;

Nowdoc strings

The syntax for the nowdoc string is the same as for the heredoc string, except that the initial identifier is enclosed in single-quotes. Variables will not be parsed inside a nowdoc string.

$s = <<<'LABEL'
Nowdoc (without parsing)
LABEL;

Escape characters

Escape characters are used to write special characters, such as backslashes or double-quotes. A table of the escape characters available in PHP can be seen below.

CharacterMeaningCharacterMeaning
\nnewline\fform feed
\thorizontal tab\$dollar sign
\vvertical tab\'single quote
\eescape\"double quote
\rcarriage return\\backslash

For example, line breaks are represented with the escape character "\n" in text.

$s = "Hello\nWorld";

Note that this character is different from the
HTML tag, which creates line breaks on web pages.

echo "Hello<br>World";

When using the single-quote or nowdoc delimiter the only escape characters that work are the backslash (\\) and single-quote (\') characters.

$s = 'It\'s'; // "It's"

Escaping the backslash is only necessary before a single-quote or at the end of the string.

Character reference

Characters within strings can be referenced by specifying the index of the desired character in square brackets after the string variable, starting with zero. This can be used both for accessing and modifying single characters.

$s = 'Hello';
$s[0] = 'J';
echo $s; // "Jello"

The strlen function retrieves the length of the string argument. This can for example be used to change the last character of a string.

$s[strlen($s)-1] = 'y';
echo $s; // "Jelly"

String compare

The way to compare two strings is simply by using the equal to operator. This will not compare the memory addresses, as in some other languages.

$a = 'test';
$b = 'test';
$c = ($a == $b); // true
Recommended additional reading:
Sams - Teach Yourself PHP in 24 Hours