CodeMIRAI
0 XP

Strings

2 min read 7 runnable examples Quiz included

Single and double quotes

PHP
<?php
$name = "Asha";
echo "Hello, $name!\n";          // double quotes: variables and \n are replaced
echo 'Hello, $name!\n';          // single quotes: everything stays exactly as typed
echo "\n";
echo "Total: {$name}'s order\n"; // braces mark where a variable name ends
  • Double quotes replace variables with their values (interpolation) and understand escapes like \n (new line) and \t (tab).
  • Single quotes take the text literally: faster to read for plain text, and safe when it contains $.

Joining strings

PHP
<?php
$first = "Ada";
$last = "Lovelace";
$full = $first . " " . $last;      // . joins strings
$full .= " (1815-1852)";           // .= appends
echo $full, "\n";
echo "Length: " . strlen($full) . "\n";

PHP joins strings with a dot, not +. Using + tries to add them as numbers.

Multi-line text: heredoc

PHP
<?php
$course = "PHP";
$lessons = 15;
$card = <<<HTML
<div class="card">
  <h2>$course</h2>
  <p>$lessons lessons</p>
</div>
HTML;
echo $card, "\n";

A heredoc (<<<NAME ... NAME;) holds multi-line text and interpolates variables, like double quotes. A nowdoc, <<<'NAME', with the name in single quotes, works like single quotes.

Everyday functions

PHP
<?php
$s = "  Learn PHP today  ";
echo "[" . trim($s) . "]\n";
echo strtoupper("php"), " ", strtolower("PHP"), " ", ucfirst("php"), " ", ucwords("hello world"), "\n";
echo str_replace("PHP", "Python", trim($s)), "\n";
echo substr("CodeMIRAI", 0, 4), " ", substr("CodeMIRAI", -5), "\n";
echo strrev("stressed"), " ", str_repeat("-", 10), "\n";
echo str_pad("7", 3, "0", STR_PAD_LEFT), "\n";

substr($s, start, length) counts from 0, and a negative start counts from the end.

Searching

PHP
<?php
$email = "asha@example.com";
var_dump(str_contains($email, "@"));          // PHP 8
var_dump(str_starts_with($email, "asha"));
var_dump(str_ends_with($email, ".org"));

$pos = strpos($email, "@");
echo "The @ is at position $pos\n";
echo "User: " . substr($email, 0, $pos) . "\n";

if (strpos("apple", "a") !== false) {        // note !== false, not != false
    echo "found an a\n";
}

strpos returns the position, or false when there's no match. A match at position 0 is also "falsy", so always compare with !== false. The PHP 8 functions str_contains, str_starts_with and str_ends_with avoid the trap entirely.

Splitting and joining

PHP
<?php
$csv = "Asha,Leo,Mei";
$names = explode(",", $csv);         // string to array
print_r($names);
echo implode(" | ", $names), "\n";   // array to string
echo count(explode(" ", "the quick brown fox")), " words\n";

Formatting

PHP
<?php
$name = "Mei";
$score = 92.456;
printf("%s scored %.1f%%\n", $name, $score);
echo sprintf("%-8s|%6.2f|", $name, $score), "\n";
echo number_format(1234567.891, 2), "\n";
echo number_format(1234567.891, 2, ".", ","), "\n";

printf prints a formatted string and sprintf returns one: %s for text, %d for whole numbers, %.2f for two decimal places, and %% for a literal percent sign. number_format adds thousands separators.

Finished this lesson?Mark it complete to earn +20 XP and move your progress on.