PHP strings - Single quoted PHP strings
The simplest form of PHP strings is the single quoted PHP string; it consists in putting the chain of characters constituting your string between single quotes (for instance, consider the PHP string $s = 'understanding').
The particularities of single quoted PHP strings are the following:
-
if you intend to have a literal single quote in your chain of characters, you need to escape it with a backlash \ in order to prevent it from being mistaken with the single quote which terminates the string.
Learn the PHP code:
<?php
$s = 'Here\'s how you do it';
echo $s;
?>
Run the PHP script in your web browser:
-
likewise, if you intend to have a backlash followed by a single quote within your chain of characters, you need to double the backlash, etc ...
Learn the PHP code:
<?php
$s = 'String terminating with (one) backlash \\';
echo $s;
?>
Run the PHP script in your web browser:
- if you intend to terminate your chain of characters with a backlash, you need to double it.
-
PHP variables within single quoted strings are not parsed.
Learn the PHP code:
<?php
$var = 1;
$s = 'The variable $var will not be parsed';
echo $s;
?>
Run the PHP script in your web browser:
PHP strings - Double quoted PHP strings
The essential difference between a single quoted PHP string and a double quoted PHP string lies in the fact that a PHP variable will be parsed in the latter and won't be parsed in the former.
Learn the PHP code:
|
<?php $var = 1; $s = "The variable $var will now be parsed"; echo $s; ?> |
Run the PHP script in your web browser:
Remark:
The double quotes must be escaped in all the cases that were already discussed above in the context of single quote strings.
Additionally, certain special characters will also need to be escaped, for instance when they need to be introduced literally in the chain of characters, which is the case for the dollar symbol $. Also, escaping the letter n will constitute a linefeed, escaping the letter r will constitute a carriage return, and escaping the letter t will constitute an horizontal tab.
PHP strings - Manipulation of PHP strings
Accessing a PHP string
Now that you know how to create a PHP string, you need to understand how to access it. Actually, a string can be seen as an array of characters, so that the n-th element of a string $s can be denoted by $s[n-1]. Thus, it is also possible to modify a string on a per character basis, just like a PHP array. For instance, you can modify the string 'love' and make it a 'dove' with the following code:
Learn the PHP code:
|
<?php $s = 'love'; $s[0] = 'd'; echo $s; ?> |
Run the PHP string in your web browser:
Concatenating two PHP strings
Two strings $s1 and $s2 can be concatenated by using . which is the concatenation operator:
Learn the PHP code:
|
<?php $s1 = 'love'; $s2 = 'ly'; echo $s1.$s2; ?> |
Run the PHP script in your web browser:
The concatenating assignment operator .=
Given two strings $s1 and $s2, the instruction $s1 .= $s2 is equivalent with $s1 = $s1.$s2.
Converting to a string
The function strval() converts an expression to a string where it is needed; for instance, strval(4) will return the string '4'.
PHP strings - PHP string functions
String length:
The length of a string $s is given by strlen($s).
The substring function:
The substring function substr() is used to extract a substring of a given length l starting at a position k from a string $s; its syntax is as follows:
substr($s,k,l)
Learn the PHP code:
|
<?php $s = 'gloves'; echo substr($s,1,4); ?> |
Run the PHP script in your web browser:
The explode function
The function explode() allows you to split a string into several substrings delimited by separators and to arrange these substrings into an array. This function was introduced in our tutorial about PHP arrays.
Searching a PHP string for a sequence of characters
The strpos() function allows you to find the position of the first occurence of a given sequence of characters within a string.
Considering a string $s and a sequence of characters given by the substring $t, the PHP syntax to follow is:
strpos($s,$t)
Learn the PHP code:
|
$s = 'gloves'; echo strpos($s,'love'); ?> |
Run the PHP script in your web browser:
PHP strings are important because they allow you to manipulate ordered sequences of characters of arbitrary lengths. This is made easy by the existence of numerous PHP string functions such as strlen, strpos, substr, etc ...
Next tutorial: PHP include / PHP require
Previous tutorial: PHP array and array of arrays
Back to computer forums
