PHP strings are sequences of bytes that you create with quotes and manipulate with a large set of built-in functions. The most useful php string functions are strlen(), strtoupper(), strtolower(), trim(), substr(), str_contains() and ucfirst(). Because PHP strings are byte-based, functions such as strlen() count bytes, not characters, which matters once you leave plain ASCII text.
This guide covers the basics you need for everyday work: how to write strings, how to join them, and which functions to reach for first. Examples target PHP 8.3 and 8.4 and work on any PHP 8.x release.
PHP offers four ways to write a string literal. Single quotes are literal, double quotes interpolate variables and understand escape sequences, and heredoc and nowdoc handle multi-line text.
Single quotes only recognise \\ and \' as escapes. Double quotes recognise \n, \t, \", \$ and others. A heredoc (<<<TXT) behaves like double quotes; a nowdoc (<<<'TXT') behaves like single quotes. Since PHP 7.3 the closing marker may be indented, and the same indentation is removed from every line.
The concatenation operator is the dot. The compound form .= appends to an existing variable, which is the usual way to build a string in a loop.
Interpolation is often cleaner than many dots: "{$first} {$last}". Note that PHP 8 uses . only for strings, and + on two non-numeric strings throws a TypeError, so never use + to join text.
strlen() returns the length in bytes. You can read a single byte with square brackets, and negative offsets count from the end.
substr($string, $offset, $length) returns a slice. Since PHP 8.0 it returns an empty string, not false, when the offset is past the end. Do not use these byte-based functions on text such as Hindi, Japanese or emoji; that case needs the mb_ functions, which a related article covers.
Case functions are handy for normalising user input before you compare or store it.
| Function | What it does | Example result for " hello WORLD " |
|---|---|---|
strtolower() | all lowercase | " hello world " |
strtoupper() | all uppercase | " HELLO WORLD " |
ucfirst() | first character uppercase | " hello WORLD " (space is first) |
ucwords() | first letter of each word uppercase | " Hello WORLD " |
trim() | remove whitespace at both ends | "hello WORLD" |
ltrim() / rtrim() | trim left or right only | one side only |
Chaining calls from the inside out is normal in PHP: trim first, lower-case next, then capitalise. trim() also accepts a second argument listing the characters to strip, for example trim($path, '/').
PHP 8.0 added three readable functions that replaced many old strpos() checks.
They are case-sensitive and return a real boolean. Compare strings for equality with ===. For an ordering comparison use strcmp(), which returns a negative number, zero or a positive number. Use strcasecmp() for a case-insensitive comparison.
strlen("é") is 2 in UTF-8, because the character takes two bytes.== to compare strings from user input. In PHP 8, "abc" == 0 is false, but numeric strings such as "1e3" == "1000" are still equal, so prefer ===.$str{0} for an offset; curly-brace offsets were removed in PHP 8.0. Use $str[0].Double quotes replace variables and process escape sequences such as \n, while single quotes keep nearly everything literally. Single quotes are slightly simpler to reason about when no interpolation is needed.
Call strlen($text) for the number of bytes, or mb_strlen($text) for the number of characters in a multibyte encoding such as UTF-8.
Use str_contains($haystack, $needle) on PHP 8.0 and later. On older versions you had to test strpos($haystack, $needle) !== false.
You can change a single byte with $s[0] = 'X', but almost every function returns a new string instead of modifying the original, so assign the result to a variable.
PHP strings are byte sequences written with single quotes, double quotes, heredoc or nowdoc. Join them with . or interpolation, measure them with strlen(), slice them with substr(), normalise them with trim() and the case functions, and search them with str_contains(), str_starts_with() and str_ends_with(). Remember that these functions work on bytes, so plan to use the multibyte functions for non-ASCII text.