PHP – Strings
Master PHP strings from creation and concatenation to 20+ powerful built-in string functions that supercharge your text processing skills.
Table of Contents
PHP Strings
A string in PHP is a sequence of characters enclosed in quotes. Strings are one of the most-used data types in PHP — used for everything from displaying HTML to querying databases.
Creating Strings
PHP supports four ways to create strings:
1. Single Quotes (Literal)
<?php
$name = 'John Doe';
echo 'Hello, $name!'; // Output: Hello, $name! (no interpolation)
?>
2. Double Quotes (With Interpolation)
<?php
$name = "Alice";
echo "Hello, $name!"; // Output: Hello, Alice!
?>
3. Heredoc
<?php
$text = <<<EOT
This is a multi-line
string using heredoc.
EOT;
echo $text;
?>
4. Nowdoc (like single-quoted heredoc)
<?php
$text = <<<'EOT'
No $variable interpolation here.
EOT;
?>
String Concatenation
Use the dot . operator to join strings:
<?php
$first = "Codes";
$last = "Compiler";
$full = $first . $last;
echo $full; // CodesCompiler
$full .= " 2025"; // Append
echo $full; // CodesCompiler 2025
?>
Essential String Functions
| Function | Purpose |
|---|---|
strlen($s) | Length of string |
strtoupper($s) | Convert to UPPERCASE |
strtolower($s) | Convert to lowercase |
str_replace($f, $r, $s) | Replace substring |
strpos($s, $n) | Find position of substring |
substr($s, $start, $len) | Extract part of string |
trim($s) | Remove whitespace from ends |
str_word_count($s) | Count words |
strrev($s) | Reverse string |
str_repeat($s, $n) | Repeat string n times |
ucfirst($s) | Capitalize first letter |
ucwords($s) | Capitalize each word |
explode($d, $s) | Split string into array |
implode($d, $a) | Join array into string |
sprintf($f, ...) | Format string |
Live String Lab
Preview
Escape Sequences in Double-Quoted Strings
| Escape | Meaning |
|---|---|
\n | Newline |
\t | Tab |
\\ | Backslash |
\" | Double quote |
\$ | Dollar sign (literal) |
<?php
echo "Line 1\nLine 2\n";
echo "Price: \$49.99\n";
echo "Path: C:\\Users\\Admin";
?>
String Formatting with sprintf()
<?php
$name = "CodesCompiler";
$price = 49.5;
$qty = 3;
$msg = sprintf(
"Order for %s: %d items at $%.2f each = $%.2f total",
$name, $qty, $price, $qty * $price
);
echo $msg;
// Order for CodesCompiler: 3 items at $49.50 each = $148.50 total
?>
PHP strings are incredibly versatile — with 100+ built-in functions, they can handle everything from basic output to complex text parsing!