← All docs

Strings

String types, escape sequences, interpolation, heredoc/nowdoc, and built-in string functions.

Double-quoted strings

Support escape sequences:

<?php
echo "Hello\n";      // newline
echo "Tab\there";    // tab
echo "Return\r";     // carriage return
echo "Vert\v";       // vertical tab
echo "Esc\e";        // escape byte
echo "Form\f";       // form feed
echo "Quote: \"";    // escaped quote
echo "Backslash: \\"; // backslash
echo "\x41";         // hex byte: A
echo "\101";         // octal byte: A
echo "\u{1F600}";    // Unicode codepoint: 😀

Single-quoted strings

No escape sequences except \\ and \':

<?php
echo 'Hello\n';      // prints: Hello\n (literal)
echo 'It\'s here';   // prints: It's here

String interpolation

<?php
$name = "World";
echo "Hello, $name\n";

Heredoc strings

Multi-line with escape processing (like double-quoted):

<?php
echo <<<EOT
Hello World
This is line 2
EOT;

Nowdoc strings

Multi-line without escape processing (like single-quoted):

<?php
echo <<<'EOT'
Hello World
No escapes: \n \t stay literal
EOT;

String indexing

<?php
$s = "hello";
echo $s[1];    // e
echo $s[-1];   // o
echo "[" . $s[99] . "]";  // []

Read-only. Negative indices count from end. Out-of-bounds returns empty string.

Built-in string functions

FunctionSignatureDescription
strlen()strlen($str): intReturns string length
substr()substr($str, $start [, $len]): stringExtract substring
strpos()strpos($hay, $needle): int|falseFind first occurrence. Returns false if not found
strrpos()strrpos($hay, $needle): int|falseFind last occurrence. Returns false if not found
strstr()strstr($hay, $needle): stringFind first occurrence and return rest
str_replace()str_replace($search, $replace, $subject): stringReplace all occurrences
str_ireplace()str_ireplace($search, $replace, $subject): stringCase-insensitive replace
substr_replace()substr_replace($str, $repl, $start [, $len]): stringReplace substring
strtolower()strtolower($str): stringConvert to lowercase
strtoupper()strtoupper($str): stringConvert to uppercase
ucfirst()ucfirst($str): stringUppercase first character
lcfirst()lcfirst($str): stringLowercase first character
ucwords()ucwords($str): stringUppercase first letter of each word
trim()trim($str [, $chars]): stringStrip whitespace from both ends
ltrim()ltrim($str [, $chars]): stringStrip whitespace from left
rtrim()rtrim($str [, $chars]): stringStrip whitespace from right
str_repeat()str_repeat($str, $times): stringRepeat a string
str_pad()str_pad($str, $len [, $pad, $type]): stringPad string to length
str_split()str_split($str [, $len]): arraySplit into chunks
strrev()strrev($str): stringReverse a string
strcmp()strcmp($a, $b): intBinary-safe string comparison
strcasecmp()strcasecmp($a, $b): intCase-insensitive comparison
str_contains()str_contains($hay, $needle): boolCheck if string contains substring
str_starts_with()str_starts_with($hay, $prefix): boolCheck prefix
str_ends_with()str_ends_with($hay, $suffix): boolCheck suffix
ord()ord($char): intASCII value of first character
chr()chr($code): stringCharacter from ASCII code
explode()explode($delim, $str): arraySplit string into array
implode()implode($glue, $arr): stringJoin array into string
number_format()number_format($n [, $dec [, $dec_point, $thou_sep]]): stringFormat number
sprintf()sprintf($fmt, ...): stringFormat string (%s, %d, %f, %x, %e, %g, %o, %c, %%)
printf()printf($fmt, ...): intFormat and print
sscanf()sscanf($str, $fmt): arrayParse string with format (%d, %s)
addslashes()addslashes($str): stringEscape quotes and backslashes
stripslashes()stripslashes($str): stringRemove escape backslashes
nl2br()nl2br($str): stringInsert <br /> before newlines
wordwrap()wordwrap($str [, $width [, $break [, $cut]]]): stringWrap text at width
bin2hex()bin2hex($str): stringConvert binary to hex
hex2bin()hex2bin($str): stringConvert hex to binary
md5()md5($str): stringMD5 hash (32-char hex)
sha1()sha1($str): stringSHA1 hash (40-char hex)
hash()hash($algo, $data): stringHash with algorithm (md5, sha1, sha256)
htmlspecialchars()htmlspecialchars($str): stringEscape HTML special chars
htmlentities()htmlentities($str): stringAlias for htmlspecialchars
html_entity_decode()html_entity_decode($str): stringDecode HTML entities
urlencode()urlencode($str): stringURL-encode (spaces as +)
urldecode()urldecode($str): stringURL-decode
rawurlencode()rawurlencode($str): stringURL-encode (spaces as %20)
rawurldecode()rawurldecode($str): stringURL-decode (RFC 3986)
base64_encode()base64_encode($str): stringBase64 encode
base64_decode()base64_decode($str): stringBase64 decode
ctype_alpha()ctype_alpha($str): boolAll chars are A-Z/a-z
ctype_digit()ctype_digit($str): boolAll chars are 0-9
ctype_alnum()ctype_alnum($str): boolAll chars are alphanumeric
ctype_space()ctype_space($str): boolAll chars are whitespace
preg_match()preg_match($pattern, $subject): intTest if regex matches (1 or 0). Uses POSIX extended regex.
preg_match_all()preg_match_all($pattern, $subject): intCount all non-overlapping matches
preg_replace()preg_replace($pattern, $replacement, $subject): stringReplace all regex matches; supports $0..$9 and \0..\9 replacement backreferences
preg_replace_callback()preg_replace_callback($pattern, $callback, $subject): stringReplace all regex matches with the callback return value; callback receives array<string> matches
preg_split()preg_split($pattern, $subject): arraySplit string by regex pattern

Regex limitations

  • Uses POSIX extended regex via libc, with translation of common PCRE shorthands (\s, \d, \w) and common Unicode property shims (\p{L}, \p{N}, \p{Lu}, \p{Ll}, and \P{...})
  • preg_replace() expands $0..$9 and \0..\9 to captured groups; unmatched optional groups expand to an empty string
  • preg_replace_callback() passes $matches[0] as the full match and $matches[1]..$matches[9] as supported capture groups
  • Lookahead, lookbehind, non-greedy quantifiers are not supported
  • preg_match() does not support $matches capture parameter