π§΅ Strings β PHP
Home: Interview Prep Index
PHP strings are immutable sequences of bytes. Most string operations return new strings. Strings can be treated like arrays for character access ($s[0]), but modification creates a new value.
Subtopics
| # | Topic | Description |
| 01 | String Basics | Creation, access, slicing, ASCII, common ops |
| 02 | PHP String Functions | strpos, str_replace, explode, preg_match and more |
| 03 | Patterns | Two pointers, sliding window, palindromes, KMP |
| 04 | Top 25 Interview Questions | Solved problems with approach sections |
The Big Picture
STRINGS IN PHP
β
βββ Basics β length, access, slice, ASCII
β βββ strlen($s)
β βββ $s[0], $s[$i]
β βββ substr($s, start, len)
β βββ ord($c), chr($n)
β
βββ Built-in Functions β PHP standard library
β βββ strpos / strrpos
β βββ str_replace / str_repeat
β βββ explode / implode
β βββ strtolower / strtoupper / ucfirst
β βββ trim / ltrim / rtrim
β βββ str_split / str_contains
β βββ preg_match / preg_replace
β
βββ Patterns β DSA on strings
β βββ Two Pointers β reverse, palindrome, two-sum
β βββ Sliding Window β longest no-repeat, min window
β βββ Hashing β anagram, frequency count
β βββ Recursion β palindrome partitioning
β
βββ Interview Questions β Top 25 problems
Complexity Cheat Sheet
| Operation | Time | Notes |
Length strlen($s) | O(n) | PHP recalculates each time |
Access $s[$i] | O(1) | Array-like access |
Concatenation . | O(n) | Creates new string |
Search strpos | O(nΓm) | Naive search |
Reverse strrev | O(n) | Built-in |
Substring substr | O(k) | k = length of result |
Split explode / str_split | O(n) | |
Replace str_replace | O(nΓm) | |
PHP vs JS String Syntax Comparison
| Operation | PHP | JavaScript |
| Length | strlen($s) | s.length |
| Access char | $s[$i] | s[i] |
| Substring | substr($s, 1, 3) | s.slice(1, 4) |
| Find | strpos($s, 'x') | s.indexOf('x') |
| Reverse | strrev($s) | s.split('').reverse().join('') |
| Split | str_split($s) | s.split('') |
| Join | implode('', $arr) | arr.join('') |
| Upper | strtoupper($s) | s.toUpperCase() |
| ASCII | ord($c) | s.charCodeAt(i) |
| Char from int | chr($n) | String.fromCharCode(n) |