Understanding PHP Variable Types
Previous | Table of Contents | Next |
An Introduction to PHP Variables | PHP Constants |
The PHP String Variable
The string variable type is used to hold strings of characters such as words and sentences. In addition to providing mechanisms for creating and changing entire string variable values, PHP allows you to extract and change parts of a string value.
A string can be assigned to variable either by surrounding it in single quotes (') or double quotes ("). If your string itself contains either single or double quotes you should use the opposite to wrap the string:
<?php $myString = "A string of text"; $myString2 = 'Another string of text'; $myString3 = "This string contains 'single quotes'"; $myString4 = 'This string contains "double quotes"'; ?>
You can also escape quotes in your string by preceding them with a backslash (\), especially useful if your string contains both single and double quotes of its own that would otherwise confuse the PHP pre-processor:
<?php $myString3 = 'This string contains \'single quotes\''; $myString4 = "This string contains \"double quotes\" and \'single quotes\'"; ?>
Double quoted strings also allow the insertion of special control sequences that are interpreted to have special meaning for the PHP pre-processor (such as a tab or new line). The following table outlines the various control sequences and their respective descriptions:
Control Sequence | Description |
---|---|
\n | New line |
\r | Carriage Return |
\t | Tab |
\\ | Backslash Character |
\" | Double quotation mark |
\$ | Dollar sign (prevents text from being treated as a variable name) |
\034 | Octal ASCII value |
\x0C | Hexadecimal ASCII Value |
As an example, we can declare a string variable which contains a tab and a new line character as follows:
<?php $myString = "This is a line of Text\nandthis is another line with a tab here \t for us."; ?>
Extracting and Writing String Fragments
Once we have defined a string variable we can extract or make changes to individual characters in the string using what is termed {x} notation, where x represents the index into the string of the character we wish to view or change. Before we look at an example, it is important to keep in mind that the index into the string is zero based. By this we mean that the first character of the string is in position 0, not position 1. For example, to change the first and last characters of a string variable:
<?php $myString = "My Bug"; $myString{0} = "m"; $myString(5] = "s"; echo $myString; ?>
The result of the above script will change the string from:
My Bug
to:
my Bus
Creating PHP heredoc Strings
The PHP heredoc string syntax allows free form text to be used without having to worry about escaping special characters such as quotes and backslashes. The content of the heredoc string is wrapped with <<<EOD and EOD; markers.
The only rules are that the closing EOD; must be at the beginning of the last line, and the only content on that line, as follows:
<?php $myString = <<<EOD This is some free form text. It can span mutliple lines and can contain otherwise troublesome characters like \ and " and ' without causing any problems. EOD; echo $myString; ?>