PHP Strings
What Is a String?
In simple terms: A string is a piece of text — a name, a sentence, an email address, an HTML snippet, or even an entire file's content.
Technically: In PHP, a string is an ordered sequence of bytes. PHP does not store an encoding with the string; it simply stores bytes. When text is saved as UTF-8 (the standard for the web), English letters take one byte each, while characters such as é, ₹, த, or emoji take two to four bytes.
Why Strings Matter
Almost everything a web application handles is text at some point:
- Form input (
$_POST), URL parameters ($_GET), and cookies always arrive as strings - HTML output sent to the browser
- JSON sent to and received from APIs
- SQL queries, file paths, email bodies, log messages
Understanding how strings are written, stored, and compared prevents a large class of bugs.
Creating Strings
PHP has four ways to write a string literal:
| Syntax | Name | Variables Interpolated? | Escape Sequences? |
|---|---|---|---|
'text' | Single-quoted | No | Only \' and |