A string is a sequence of characters enclosed in single quotes '...' or double quotes "...". Double quotes support variable interpolation and special escape characters (such as , ), whereas single quotes render raw literal text.
$name = "Alice";
// Double-quoted interpolation
echo "Hello $name
";
// Heredoc (Interpolated multi-line string)
$html = <<<HTML
<div class="user">
<p>Name: $name</p>
</div>
HTML;
// Nowdoc (Literal multi-line string)
$code = <<<'CODE'
$literalVariable = "Not evaluated";
CODE;
flowchart TD
A["Input String"] --> B["Length Check: strlen() / mb_strlen()"]
A --> C["Search & Position: strpos() / str_contains()"]
A --> D["Transformation: strtoupper() / trim() / str_replace()"]
A --> E["Extraction: substr() / explode()"]
<?php
declare(strict_types=1);
$rawEmail = " [email protected] ";
// 1. Trim whitespace
$cleanEmail = trim($rawEmail);
// 2. Convert to lowercase
$normalizedEmail = strtolower($cleanEmail);
// 3. String Search & Inspection (PHP 8 Helpers)
$containsDomain = str_contains($normalizedEmail, "kodersolution.com");
$startsWithUser = str_starts_with($normalizedEmail, "user");
// 4. String Replacement & Split
$sanitizedEmail = str_replace(" ", "", $normalizedEmail);
$parts = explode("@", $sanitizedEmail);
echo "Original String: '$rawEmail'
";
echo "Clean Email: '$normalizedEmail'
";
echo "Username Part: '" . $parts[0] . "'
";
echo "Domain Part: '" . $parts[1] . "'
";
echo "Valid Domain?: " . ($containsDomain ? "Yes" : "No") . "
";
mb_* Functions for Multibyte Unicode Strings: Use mb_strlen(), mb_strtolower() when dealing with international non-ASCII characters.str_contains(), str_starts_with(), and str_ends_with() over legacy strpos() !== false checks.Write a function maskEmail(string $email): string that takes an email address (e.g. [email protected]) and masks the username part (e.g. a***@example.com) using explode() and substr()!
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.