Introduction
The nature of web development requires strings to be created, manipulated and,
in some cases, evaluated. Understanding the concept of string manipulation is an
important piece for developers.
This introductory tutorial will explain some simple constructs associated to
handling strings in addition to some more advanced techniques for those of you
who are ambitious.
This tutorial assumes several things about the reader:
- You are familiar with PHP
- You have access to a server or servers running the PHP
package
Quoted Strings
1
2
3
4
|
<?php
$str_1 = 'I
am a string in single quotes';
$str_2 =
"I am a string in double quotes";
?> |
|
The easiest way to specify a simple string is to enclose it
in single quotes (the character '). Enclosing the string in double-quotes ("),
will allow PHP to understand additional escape sequences for special
characters:
\n linefeed (LF or 0x0A (10) in ASCII)
\r carriage return (CR or 0x0D (13) in ASCII)
\t horizontal tab (HT or 0x09 (9) in ASCII)
\\ backslash
\$ dollar sign
\" double-quote
\[0-7]{1,3} the sequence of characters matching the regular expression is a
character in octal notation
\x[0-9A-Fa-f]{1,2} the sequence of characters matching the regular expression
is a character in hexadecimal notation
The PHP parser will determine string start and end points based on matching
quotes. That means every string must start and end with the same type of
quote. This is the cause of many headaches for some coders. You can include
single quote marks and other characters in the double quoted string, which
often lends confusion to when you have actually closed your string.
Here's a trick I use to ensure I avoid this problem... When I create a string
variable, I complete the entire declaration statement prior to filling in a
value:
1
2
3
4
|
<?php
$str_1 = '';
$str_2 =
"";
?> |
|
Then follow through by adding my value to the statement:
1
2
3
4
|
<?php
$str_1 = 'I
am a string in single quotes';
$str_2 =
"I am a string in double quotes";
?> |
|
Escape Characters
To include a quote in the middle of a string and have the parser ignore it you
need to escape it with a backslash. This tells PHP that the quote should not be
used as the end point of the string, and to continue to the next quote:
1
2
3
4
5
6
|
<?php
$str_1 = 'I am
a \'string\' in single quotes';
echo $str_1."<br>";
$str_2 =
"I am a \"string\" in double quotes";
echo $str_2."<br>";
?> |
|
To include a backslash itself in a string, you need to escape
the backslash with a backslash:
1
2
3
4
5
6
|
<?php
$str_1 = 'I am
a \\backslash\\ in single quotes';
echo $str_1."<br>";
$str_2 =
"I am a \\backslash\\ in double quotes";
echo $str_2."<br>";
?> |
|
Another way to delimit strings is by using heredoc syntax
("<<<"). Using this syntax a unique identifier is placed after <<<, the string
to be stored or displayed, and finally the same identifier to close the
quotation.
The closing identifier must begin in the first column of the line. Also, the
identifier used must follow the same naming rules as any other label in PHP: it
must contain only alphanumeric characters and underscores, and must start with a
non-digit character or underscore.
1
2
3
4
5
6
7
|
<?php
$str = <<<EOD
Example of a string
spanning multiple lines
using the heredoc syntax.
EOD;
?> |
|
The heredoc system is also known as the Here Document
format. This system is normally used for large amounts of text, with no worries
of overlapping quotes. You do not need to escape quotes in your here docs, but
you can still use escape codes.
Concatenating Strings
Using the concatenate operator (.), strings can be easily joined together.
1
2
3
4
5
6
7
|
<?php
$str_1 = 'This';
$str_2 =
'is a';
$str_3 =
'string.';
$str =
$str_1.
" " .$str_2.
" " .$str_3;
echo $str;
?> |
|
Used with the assignment operator (=), you can append data to
your string - making the creation of large strings very simple.
1
2
3
4
5
6
|
<?php
$str = 'This ';
$str .=
'is a ';
$str .=
'string.';
echo $str;
?> |
|
Variables & Strings
PHP lets you include variables inside double quoted strings:
1
2
3
4
5
6
|
<?php
$str_1 = 'This
is a';
$str_2 =
'string.';
$str =
"$str_1 $str_2";
echo $str;
?> |
|
Double and single quoted strings containing variables will not
give you the same results. Double quoted strings will interpret the variable
while single quoted strings will treat it as part of the original string
variable:
1
2
3
4
5
6
7
|
<?php
$num = 9;
echo 'Display the number $num';
// displays "Display the number $num" to
the screen
echo "Display
the number $num";
// displays "Display the number 9" to the
screen
?> |
|
Because scripts using single quotes run slightly faster,
double quotes should only be used when you want to include special escape
characters or variables within the string.
It is advisable to concatenate strings, as shown in prior examples, when you are
using complex or even simple variable substitution.
1
2
3
4
5
6
|
<?php
$str_1 = 'This
is a';
$str_2 =
'nother boring';
$str_3 =
'string example';
echo $str_1.$str_2.
" " .$str_3.
".";
?> |
|
Conclusion
When working with strings, don't make the PHP interpreter have to work any
harder than it already is. A large portion of web development involves handling
and generating strings, knowing how to properly handle and manipulate strings is
important. To maximize your code, correct string definition is the first, and
often forgotten, place to start.
Always remember to use single quotes to enclose your strings, unless including
variables within the string variable. And when you need to involve variables,
try to make use of concatenation to avoid using double quotes wherever possible.
Your end result will be a much faster site.