Strings in php

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 sequenceCharacter represented
\”Double quotes
\nNewline
\rCarriage return
\tTab
\\Backslash
\$Dollar sign
\{Left brace
\}Right brace
\[Left bracket
\]Right bracket
\0 through \777ASCII character represented by octal value
\x0 through \xFFASCII character represented by hex value
Table of Double quotes in strings

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
}

Leave a Comment