PHP Reference ยท Types and Type Handling
The PHP string type
Reference for the PHP string type: single versus double quotes, escape sequences, variable interpolation, byte-based indexing, UTF-8 and conversion rules.
A string is a sequence of bytes. PHP does not attach an encoding to it: the bytes mean whatever you decide, and in modern code that is UTF-8. Because of this, strlen("รฉ") is 2, and functions without an mb_ prefix work on bytes, not characters.
You write strings with single quotes, double quotes, heredoc or nowdoc. Single quotes are literal. Double quotes and heredoc process escape sequences and insert variables, which is called interpolation.
$name = "Ada"; echo 'Hi $name\n'; // Hi $name\n (literal) echo "Hi $name\n"; // Hi Ada + newline
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
'single quoted' "double quoted $var {$expr}" <<<TAG ... TAG; <<<'TAG' ... TAG; // heredoc and nowdoc $s[0] $s[-1] // read or write one byte $s . $t $s .= $t // concatenation
| Escape (double quotes) | Meaning |
|---|---|
\n, \r, \t |
Newline, carriage return, tab |
\\, \", \$ |
Backslash, double quote, dollar sign |
\x41, \101 |
Byte by hex or octal code |
\u{1F600} |
UTF-8 encoding of a code point |
\e, \v, \f |
Escape, vertical tab, form feed |
In single quotes only \' and \\ are escapes.
Usage notes
- Interpolation rules.
"$a[0]"and"$obj->name"take one level. For anything deeper, method calls or expressions, use"{$a['x']['y']}"or"{$obj->method()}". The old"${var}"form is deprecated since PHP 8.2. - Strings are byte strings. Use
mb_strlen(),mb_substr()and friends for text in UTF-8. See Multibyte string functions. - Indexing a string gives a one-byte string, not a character. A negative index counts from the end. Reading an out-of-range offset emits a warning and yields
""; assigning past the end pads with spaces. - Immutable values, mutable variables.
$s[0] = "J"replaces one byte in place. Assigning multiple characters stores only the first. - Concatenation uses
., not+. Since PHP 8,.has lower precedence than+and-. See String operators. - Number conversion. Numeric strings (
"12","1.5","1e3") behave as numbers in arithmetic; leading-numeric strings ("12abc") warn; others throwTypeError."abc" == 0isfalsein PHP 8. - Heredoc closing marker may be indented (PHP 7.3); that indentation is removed from every line. Nowdoc is a heredoc without interpolation.
- Strings from
Stringableobjects. An object with__toString()converts to string automatically. - Performance. Building large strings with
.=in a loop is fine; PHP over-allocates. Joining an array withimplode()is also fast.