PHP includes core-level support for creating and manipulating strings. A string is a sequence of characters of arbitrary length. String literals are delimited by either single or double quotes:
‘big dog’
“fat hog”
Variables are expanded (interpolated) within double quotes, while within single quotes they are not:
$name = “Guido”;
echo “Hi, $name\n”;
echo ‘Hi, $name’;
Hi, Guido
Hi, $name
Table of Double quotes in strings
Escape sequence | Character represented |
---|---|
\” | Double quotes |
\n | Newline |
\r | Carriage return |
\t | Tab |
\\ | Backslash |
\$ | Dollar sign |
\{ | Left brace |
\} | Right brace |
\[ | Left bracket |
\] | Right bracket |
\0 through \777 | ASCII character represented by octal value |
\x0 through \xFF | ASCII character represented by hex value |
A single-quoted string recognizes \ to get a literal backslash and \’ to get a literal single quote:
$dosPath = ‘C:\WINDOWS\SYSTEM’;
$publisher = ‘Tim O\’Reilly’;
echo “$dosPath $publisher\n”;
C:\WINDOWS\SYSTEM Tim O’Reilly
To test whether two strings are equal, use the == (double equals) comparison operator:
if ($a == $b) {
echo “a and b are equal”
}
Use the is_string() function to test whether a value is a string:
if (is_string($x)) {
// $x is a string
}