A PHP string is a sequence of bytes representing text, wrapped in either single quotes ('...') or double quotes ("..."). Strings power everything from user names and URL slugs to HTML templates and JSON payloads.
Single-quoted strings treat text literally and do not parse variables or escape sequences other than \' and \\. Double-quoted strings parse variables ("$name") and special characters like
for newlines.
<?php
declare(strict_types=1);
$framework = "Laravel";
echo 'Literal output: $framework
' . PHP_EOL;
echo "Interpolated output: Welcome to {$framework}!" . PHP_EOL;
Wrap variables in curly braces ("{$framework}") inside double quotes so PHP knows exactly where the variable name stops.
PHP ships with fast built-in functions for measuring, searching, trimming, and replacing text. PHP 8 also introduced readable search helpers like str_contains(), str_starts_with(), and str_ends_with().
<?php
declare(strict_types=1);
$rawInput = " [email protected] ";
$cleanEmail = strtolower(trim($rawInput));
echo "Normalized: {$cleanEmail}" . PHP_EOL;
echo "Length: " . strlen($cleanEmail) . PHP_EOL;
echo "Has @ sign: " . (str_contains($cleanEmail, '@') ? 'Yes' : 'No') . PHP_EOL;
echo "Ends with .com: " . (str_ends_with($cleanEmail, '.com') ? 'Yes' : 'No') . PHP_EOL;
$slug = str_replace(' ', '-', strtolower("Learn PHP Strings Today"));
echo "URL Slug: {$slug}" . PHP_EOL;
For international text containing emojis or accented characters like é or বাংলা, use multibyte functions such as mb_strlen() and mb_substr() instead of strlen().
strlen() on UTF-8 text: strlen("Café") returns 5 bytes instead of 4 characters. Use mb_strlen($text, 'UTF-8').strpos() == false: strpos() returns 0 when a match starts at index 0, and 0 == false evaluates to true. Use strict !== false or str_contains().+ to join strings: In PHP, + adds numbers. Always use the dot operator . to concatenate strings.Use explode(',', $csvLine) to break a string into an array, and implode(', ', $items) to join array items back into a string.
Heredoc (<<<TEXT ... TEXT;) lets you write multi-line strings with variable interpolation without escaping quotes inside the block.
str_replace() modify the original string variable?No. PHP string functions return a brand-new string and leave the original variable unchanged unless you reassign it.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.