Learn Strings in PHP

A string is text. Double quotes replace a $variable with its value and turn \n into a new line, single quotes write everything as it is ('Hello $name' stays…

If you've written JavaScript before, your hands will type "Total: " + 36 without thinking and expect Total: 36. PHP stops with a TypeError instead. In PHP, text is joined with a dot. The dot is only one of several string rules in PHP that differ from JavaScript.

A string is text: a menu item's name, a receipt, an error message, even the whole HTML page PHP sends. Picture a receipt at Mrs. Tini's Stall. Its sentence is always the same, "Order: ... portions of ..., total $...". Only the numbers and the menu item change with each order. In PHP, the part of the sentence that never changes is written as pieces of text, and the parts that change come from variables. Then you can work with the text using PHP's built-in functions: count its length, replace a word, or turn it into capitals.

Double quotes and single quotes

This code writes the same sentence twice: with double quotes, then with single quotes.

<?php
$menu = "Chicken Noodles";
$quantity = 3;

echo "Order $quantity portions of $menu\n";
echo 'Order $quantity portions of $menu\n';
Order 3 portions of Chicken Noodles
Order $quantity portions of $menu\n

Joining with the dot

This receipt is built from pieces of text and variables:

<?php
$menu = "Chicken Noodles";
$total = 36;

$receipt = "Order: " . $menu . ", total \$" . $total;
$receipt .= " (paid)";

echo $receipt;
Order: Chicken Noodles, total $36 (paid)

Tidy numbers on a receipt. A price on a receipt shows its cents: 36.00, not 36. number_format sets the decimals and the thousands separators, and printf lines up the columns:

<?php
$total = 36;

echo number_format($total, 2) . "\n";
echo number_format($total, 2, ",", ".") . "\n";
printf("%-16s %8s\n", "Chicken Noodles", number_format($total, 2));
printf("%-16s %8s\n", "Iced Tea", number_format(10, 2));
36.00
36,00
Chicken Noodles     36.00
Iced Tea            10.00

A heredoc for text over several lines

A receipt of two lines can be written as a heredoc: you write the text line by line, with no dots and no \n.

<?php
$menu = "Chicken Noodles";
$quantity = 3;
$total = 36;

$receipt = <<<RECEIPT
Order: $quantity portions of $menu
Total: \$$total
RECEIPT;

echo $receipt;
Order: 3 portions of Chicken Noodles
Total: $36

String functions you'll use a lot

These six built-in functions work on one sentence:

<?php
$sentence = "I Am Learning PHP";

echo strlen($sentence) . "\n";
echo strtolower($sentence) . "\n";
echo strtoupper($sentence) . "\n";
echo str_replace("PHP", "Laravel", $sentence) . "\n";
echo substr($sentence, 5, 8) . "\n";
var_dump(str_contains($sentence, "PHP"));
17
i am learning php
I AM LEARNING PHP
I Am Learning Laravel
Learning
bool(true)

strlen counts bytes

"Café Crème" has 10 characters, the space included. Before looking at the result, guess: does strlen answer 10 or 12?

<?php
$menu = "Café Crème";

echo strlen($menu) . "\n";
echo mb_strlen($menu) . "\n";
echo strtoupper($menu) . "\n";
echo mb_strtoupper($menu) . "\n";
12
10
CAFé CRèME
CAFÉ CRÈME

⚠️ Wrong joins and wrong quotes

TL;DR: A string is text. Double quotes replace a $variable with its value and turn \n into a new line, single quotes write everything as it is ('Hello $name' stays Hello $name). Text is joined with the dot (.), not +, and .= adds text to the end of a variable. Text over several lines is neatest as a heredoc <<<RECEIPT ... RECEIPT;. strlen counts the length, spaces included ("I Am Learning PHP" = 17, "Learning" = 8). strtolower/strtoupper change the letters to lower/upper case, and str_replace(search, replace, text) returns a new string and leaves the original as it was. strlen counts bytes (units of data), and an accented letter takes 2 bytes. So for accented text such as "Café Crème", use mb_strlen and the other mb_ functions.

⚡ Double quotes or single quotes

Write:

<?php
$menu = "Chicken Noodles";
$quantity = 3;

echo "Order $quantity portions of $menu\n";
echo 'Order $quantity portions of $menu\n';
Guess first: what does the second echo (with single quotes) write?

Order $quantity portions of $menu\n, exactly as it is. In single quotes, variables and \n (the new line sign) aren't processed. The first echo writes Order 3 portions of Chicken Noodles.

Meaning: A sentence that uses variables goes in double quotes, because only double quotes replace a variable with its value.

⚡ Joining with the dot

Write:

$receipt = "Order: " . $menu . ", total \$" . $total;
$receipt .= " (paid)";

Meaning: $menu and $total hold the menu item's name and the total price. . joins two strings, and .= adds text to the end of the text already in $receipt. Don't use + as in JavaScript: the result is a TypeError.

⚡ String functions

Write:

<?php
$sentence = "I Am Learning PHP";

echo strlen($sentence) . "\n";
echo strtolower($sentence) . "\n";
echo str_replace("PHP", "Laravel", $sentence) . "\n";

Meaning: strlen counts the length, spaces included: 17. strtolower makes every letter lower case. str_replace returns a new string, with "PHP" replaced by "Laravel". $sentence itself doesn't change, so the result has to be echoed or stored.

⚡ strlen counts bytes

Guess first: "Café Crème" has 10 characters, the space included. What does strlen answer?

12. An accented letter like é takes 2 bytes (units of data), and strlen counts bytes. mb_strlen answers 10.

Meaning: For accented text, use the functions that start with mb_, such as mb_strlen, which counts characters, not bytes.

What you will learn