Lesson

PHP Strings

10 min reading
Free Course

PHP Strings: Working with Text and String Functions

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 Quotes vs Double Quotes

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.

Essential PHP 8 String Functions

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 = "  hello@KoderSolution.com  ";
$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().

Common Mistakes to Avoid

  • Using strlen() on UTF-8 text: strlen("Café") returns 5 bytes instead of 4 characters. Use mb_strlen($text, 'UTF-8').
  • Comparing strpos() == false: strpos() returns 0 when a match starts at index 0, and 0 == false evaluates to true. Use strict !== false or str_contains().
  • Using + to join strings: In PHP, + adds numbers. Always use the dot operator . to concatenate strings.

Frequently Asked Questions

How do I split a comma-separated string into an array?

Use explode(',', $csvLine) to break a string into an array, and implode(', ', $items) to join array items back into a string.

What is Heredoc syntax in PHP?

Heredoc (<<<TEXT ... TEXT;) lets you write multi-line strings with variable interpolation without escaping quotes inside the block.

Does 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.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
PHP Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum