Aral Code

Basics Lesson 5 of 17

PHP Strings and String Functions

Single vs double quotes, joining with the dot operator, and essential string functions — strlen, strtoupper, ucwords, trim, str_replace, strpos, substr, explode, implode, number_format and sprintf.

Most of what PHP sends to the browser is text, so string handling is a core PHP skill. PHP has a huge set of built-in string functions.

Single vs double quotes

<?php
$name = "Ana";
echo "Hello, $name!\n";       // double quotes: variables and \n work
echo 'Hello, $name!\n';       // single quotes: everything is literal
echo "\n";
echo "Hello, {$name}s!";      // braces separate the variable from the text

Output

Hello, Ana!
Hello, $name!\n
Hello, Anas!

Joining strings

<?php
$first = "Andres";
$last = "Bonifacio";
$full = $first . " " . $last;
$full .= " (1863)";
echo $full;

Output

Andres Bonifacio (1863)

Essential functions

<?php
$s = "  maria clara santos  ";
$clean = trim($s);
echo strlen($clean), "\n";
echo strtoupper($clean), "\n";
echo ucwords($clean), "\n";
echo ucfirst($clean), "\n";
echo str_replace("santos", "reyes", $clean), "\n";
echo strpos($clean, "clara"), "\n";
echo substr($clean, 0, 5), "\n";
echo substr($clean, -6), "\n";
echo str_contains($clean, "clara") ? "yes" : "no", "\n";
echo str_repeat("=", 10), "\n";

Output

18
MARIA CLARA SANTOS
Maria Clara Santos
Maria clara santos
maria clara reyes
6
maria
santos
yes
==========
FunctionDoes
strlen($s)Length (in bytes)
trim($s)Remove spaces at both ends
strtoupper, strtolower, ucwords, ucfirstChange case
str_replace($find, $replace, $s)Replace text
strpos($s, $find)Position, or false if not found
substr($s, $start, $length)Part of a string
str_contains, str_starts_with, str_ends_withtrue/false (PHP 8)

explode and implode

<?php
$csv = "Math,Science,English";
$subjects = explode(",", $csv);       // string → array
print_r($subjects);
echo implode(" | ", $subjects);       // array → string

Output

Array
(
    [0] => Math
    [1] => Science
    [2] => English
)
Math | Science | English

Formatting numbers and text

<?php
echo number_format(1234567.891, 2), "\n";
echo "₱" . number_format(250), "\n";
echo sprintf("%-10s|%6.2f", "Kape", 8), "\n";
echo sprintf("Receipt #%05d", 42), "\n";
echo str_pad("7", 3, "0", STR_PAD_LEFT), "\n";

Output

1,234,567.89
₱250
Kape      |  8.00
Receipt #00042
007

Text with ñ and emojis

strlen counts bytes, and characters like ñ use two bytes in UTF-8. For correct character counts, use the mb_ (multibyte) functions:

<?php
$word = "Señor";
echo strlen($word), " bytes, ", mb_strlen($word), " characters\n";
echo mb_strtoupper($word);

Output

6 bytes, 5 characters
SEÑOR

Heredoc for long text

<?php
$name = "Juan";
$total = 111;
echo <<<TEXT
Dear $name,
Your total is ₱$total.
Salamat po!
TEXT;

Output

Dear Juan,
Your total is ₱111.
Salamat po!

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does 'Hi $name' (single quotes) output?
2. Which function splits a string into an array?
3. What does number_format(1500.5, 2) return?
4. Why compare strpos with !== false?

By Sir Reyel · Last updated 2026-12-04