PHP strings come with a few quoting styles and a large library of built-in functions — this lesson covers the ones you'll actually use day to day.
Single quotes take everything literally; double quotes interpret variables and a handful of escape sequences like \n (newline):
<?php
$name = "Priya";
echo 'Hello, $name'; // literally: Hello, $name
echo "Hello, $name"; // Hello, Priya
echo "Line one\nLine two"; // \n becomes an actual newline
?>When a string has no variables to interpolate, single quotes are marginally faster and a common style choice — but this is a minor detail, not a rule.
For a long block of text with variables mixed in, heredoc syntax avoids piling up quotes and dots:
<?php
$user = "Amit";
$message = <<<EOT
Hello, $user.
Thanks for signing up.
EOT;
echo $message;
?>| Function | What it does | Example |
|---|---|---|
| strlen($s) | Length of a string | strlen("hello") → 5 |
| strtoupper($s) / strtolower($s) | Change case | strtoupper("hi") → "HI" |
| trim($s) | Removes whitespace from both ends | trim(" hi ") → "hi" |
| str_replace($find, $replace, $s) | Replace all occurrences | str_replace("cat", "dog", "I like cat") → "I like dog" |
| substr($s, $start, $length) | Extract part of a string | substr("hello", 1, 3) → "ell" |
| explode($delimiter, $s) | Split a string into an array | explode(",", "a,b,c") → ["a","b","c"] |
| implode($glue, $array) | Join an array into a string | implode("-", ["a","b"]) → "a-b" |
PHP has well over a hundred string functions in total — see the full string function reference on php.net for anything not covered here.
Strings are zero-indexed too
PHP array and string indexes both start at 0, not 1 — the first character of a string is at position 0. This matches almost every language you're likely to learn next.