PHP String
Learn how strings work in PHP - creating strings, single vs double quotes, heredoc/nowdoc, escape sequences, concatenation, and the most-used string functions with examples.
A string in PHP is a sequence of characters - text such as a name, a sentence, or an entire HTML document. Strings are one of PHP's most-used data types, and the language ships with a large library of functions for searching, replacing, formatting, and transforming them.
This page covers how to create strings, the difference between single and double quotes, escape sequences, joining strings together, and the string functions you will reach for most often.
Creating a string
You can write a string literal using single quotes (') or double quotes ("):
<?php
$greeting = 'Hello';
$name = "World";
echo $greeting; // Hello
?>For longer or more complex text, PHP also offers heredoc and nowdoc syntax, described below.
Single vs. double quotes
This is the most important distinction to learn early. Double-quoted strings parse variables and escape sequences; single-quoted strings do not.
<?php
$name = "Alice";
echo "Hello, $name\n"; // Hello, Alice (then a newline)
echo 'Hello, $name\n'; // Hello, $name\n (printed literally)
?>In the double-quoted version, $name is replaced by its value and \n becomes a real newline. In the single-quoted version, both are output exactly as typed.
Single quotes are slightly faster and safer when you do not want interpolation, so use them for plain literals and switch to double quotes only when you need a variable or an escape sequence inside the string.
Curly-brace interpolation
When a variable is followed by characters that could be part of its name, wrap it in {} so PHP knows where the name ends:
<?php
$fruit = "apple";
echo "I ate two {$fruit}s\n"; // I ate two apples
?>Escape sequences
Escape sequences only have meaning inside double-quoted strings (and heredoc):
| Sequence | Meaning |
|---|---|
\n | Newline |
\t | Tab |
\\ | Backslash |
\" | Double quote |
\$ | Dollar sign (prevents interpolation) |
<?php
echo "Line one\nLine two\n";
echo "Price: \$10\n"; // Price: $10
?>Heredoc and nowdoc
For multi-line text, heredoc behaves like a double-quoted string (variables are parsed) and nowdoc behaves like a single-quoted string (nothing is parsed).
<?php
$name = "Sam";
// Heredoc - interpolates $name
echo <<<EOT
Dear $name,
Welcome aboard!
EOT;
echo "\n---\n";
// Nowdoc - prints $name literally
echo <<<'EOT'
Dear $name,
This is shown verbatim.
EOT;
?>The closing identifier (EOT here) must start at the beginning of a line.
Concatenation
Join strings with the dot (.) operator. The .= operator appends to an existing string:
<?php
$first = "John";
$last = "Doe";
$full = $first . " " . $last;
echo $full . "\n"; // John Doe
$message = "Hello";
$message .= ", world!";
echo $message . "\n"; // Hello, world!
?>See PHP Operators for the full list of string and arithmetic operators.
Common string functions
PHP's standard library includes dozens of string functions. These are the ones you will use most:
<?php
$text = "Hello, World";
echo strlen($text) . "\n"; // 12 - length in bytes
echo strtoupper($text) . "\n"; // HELLO, WORLD
echo strtolower($text) . "\n"; // hello, world
echo str_replace("World", "PHP", $text) . "\n"; // Hello, PHP
echo strpos($text, "World") . "\n"; // 7 - index of first match
echo substr($text, 0, 5) . "\n"; // Hello
echo trim(" padded ") . "|\n"; // padded|
?>| Function | What it does |
|---|---|
strlen() | Returns the string's length |
strtoupper() / strtolower() | Changes case |
str_replace() | Replaces all occurrences of a substring |
strpos() | Finds the position of a substring (returns false if absent) |
substr() | Extracts part of a string |
trim() | Removes whitespace from both ends |
explode() | Splits a string into an array |
implode() | Joins an array into a string |
Formatting with sprintf()
When you need precise control over how values are placed inside a string - padding, number formatting, alignment - use sprintf(). It returns a formatted string (and printf() prints it directly):
<?php
$name = "Alice";
$score = 92.5;
$line = sprintf("%s scored %.1f%%", $name, $score);
echo $line . "\n"; // Alice scored 92.5%
?>The %s placeholder inserts a string, %d an integer, and %.1f a float rounded to one decimal place.
Accessing individual characters
A string can be indexed like an array, starting at 0:
<?php
$word = "PHP";
echo $word[0] . "\n"; // P
echo $word[2] . "\n"; // P
?>Where to go next
- PHP Strings - a deeper tutorial on working with text in PHP.
- PHP Data Types - how strings fit alongside integers, floats, arrays, and more.
- PHP Functions - write your own reusable functions.