Strings in PHP
Master string handling in PHP: single-quoted, double-quoted, heredoc, nowdoc, interpolation, indexing, traversal, searching, slicing, comparing, replacing, and common string functions.
Creating Strings
PHP has four ways to create strings: single quotes (`''`), double quotes (`""`), heredoc (`<<<LABEL`), and nowdoc (`<<<'LABEL'`). Each behaves differently regarding variable interpolation and escape sequences.
1. Single-quoted Strings
The simplest form — no interpolation, almost no escape sequences. What you type is literally what you get.
2. Double-quoted Strings
Support variable interpolation and all escape sequences — the most commonly used string style.
3. Heredoc (<<<LABEL)
A multiline string that behaves like a double-quoted string — supports interpolation and escape sequences.
4. Nowdoc (<<<'LABEL')
A multiline string that behaves like a single-quoted string — NO interpolation whatsoever.
String Basics (Indexing, Length, Traversal)
PHP strings are sequences of bytes. You can access individual characters by index, get the total length with `strlen()`, and traverse every character with a `for` loop or `str_split()`.
1. Accessing a Character at a Specific Index
Use `$str[index]` or `substr()` to access a character. Index starts from 0.
2. Getting String Length
`strlen()` returns the byte length. For multibyte (UTF-8) strings use `mb_strlen()`.
3. Traversing Through a String
Loop through each character using a `for` loop or `str_split()`.
String Interpolation
PHP supports several interpolation styles inside double-quoted strings and heredocs. Understanding all forms helps you write clean, readable output without messy concatenation.
1. Simple Variable Interpolation
PHP replaces `$varName` directly inside double-quoted strings.
2. Curly-brace Interpolation `{$var}`
Curly braces make the variable boundary explicit and allow array/property access inside strings.
3. Complex (Variable Variable) Interpolation `${expr}`
The `${expr}` form evaluates the expression inside braces as a variable name — useful for variable variables.
Comparing & Concatenating Strings
PHP uses the dot (`.`) operator for concatenation and provides `strcmp()`, `strcasecmp()`, and the spaceship operator for comparisons.
1. Concatenation with . and .=
The `.` operator joins two strings. `.=` appends to an existing variable.
2. Comparing Strings (Exact & Case-insensitive)
`strcmp()` for case-sensitive comparison, `strcasecmp()` for case-insensitive.
3. Repeating a String (str_repeat)
`str_repeat()` returns a string repeated a given number of times.
String Transformations
PHP provides a rich set of functions to transform string values — converting case, reversing, padding to a fixed width, and wrapping long text.
1. Case Conversion
`strtoupper()`, `strtolower()`, `ucfirst()`, `ucwords()` cover all common case needs.
2. Reversing a String (strrev)
`strrev()` returns the string in reverse character order.
3. Padding Strings (str_pad)
`str_pad()` pads a string to a given length — left, right, or both sides.
4. Wrapping Long Text (wordwrap)
`wordwrap()` breaks a string into lines of a given maximum width.
Slicing, Substrings, Splitting & Trimming
These operations are essential for parsing URLs, emails, CSV data, and user input.
1. substr() — Extract a Substring
`substr($str, $start, $length)` extracts a portion of the string.
2. explode() and implode() — Split & Join
`explode()` splits a string into an array; `implode()` joins an array into a string.
3. Trimming Whitespace (trim, ltrim, rtrim)
Remove leading and/or trailing whitespace or custom characters.
Searching & Replacing in Strings
PHP provides a complete toolkit for string searching — from simple `strpos()` to regex-based `preg_match()`. PHP 8.0 added `str_contains()`, `str_starts_with()`, and `str_ends_with()` for cleaner intent.
1. strpos() and strrpos() — Find Position
`strpos()` finds the first occurrence; `strrpos()` finds the last.
2. str_contains, str_starts_with, str_ends_with — PHP 8.0+
Clean, readable helpers that return `true`/`false` without the `!== false` pattern.
3. str_replace() and str_ireplace()
`str_replace()` replaces all occurrences of a substring. `str_ireplace()` is case-insensitive.
4. sprintf() and number_format()
Format values into strings with precise control over decimals, padding, and separators.
ASCII & Character Encoding
PHP provides `ord()` and `chr()` for converting between a character and its ASCII/Unicode code point.
1. ord() and chr() — Character ↔ Code
`ord()` returns the ASCII value of a character; `chr()` converts a code back to a character.