Skip to content
Theory · Strings · lesson 4 of 9

String functions

Besides printing a string, you can take it apart: cut the year out of a date, replace a word, check whether an address contains an @. PHP has ready-made functions for this, and this lesson collects the most useful ones.

Back to the row of tiles. Every tile in a string has a number, and the numbers start at zero, like the elements of a list. You can take a single tile by its number, and functions can count the tiles, cut out a piece of the row and lay out a new row.

Length and single characters

Let's take a date apart:

PHP
<?php

$date = '2026-10-23';
echo strlen($date), "\n"; // 10
echo $date[0], ' ', $date[-1], "\n"; // 2 3
echo substr($date, 0, 4), "\n"; // 2026
echo substr($date, 5, 2), "\n"; // 10
echo substr($date, -2), "\n"; // 23

The first two lines of output:

  • strlen($date) returns the length of the string, the number of tiles: the date has 10, hyphens included;
  • $date[0] is tile number 0, that is, the first character;
  • a negative number counts from the end, and $date[-1] is the last character.

A piece of a string: substr

The function substr($s, $start, $length) cuts a piece out of a string:

  • $start is the number of the piece's first tile; a negative number counts from the end;
  • $length is how many tiles to take; without it the piece runs to the end of the string.

The example has three calls:

  1. substr($date, 0, 4) takes four tiles from the start, which is the year.
  2. substr($date, 5, 2) starts at tile number 5 and takes two, the month.
  3. substr($date, -2) starts two tiles before the end and runs to the end, the day.

A new string instead of the old one

The other functions change the text: they remove spaces, replace words, switch letters to another case.

PHP
$text = '  hello, world  ';
$clean = trim($text);
echo '[', $clean, ']', "\n"; // [hello, world]
echo str_replace('world', 'PHP', $clean), "\n"; // hello, PHP
echo strtoupper($clean), "\n"; // HELLO, WORLD
echo ucfirst($clean), "\n"; // Hello, world
echo str_repeat('-', 5), ' ', strrev('abc'), "\n"; // ----- cba
echo '[', $text, ']', "\n"; // [  hello, world  ]

What each function does:

  • trim removes spaces and line breaks at the start and at the end of a string;
  • str_replace('world', 'PHP', $clean) replaces every world with PHP: first what to find, then what to put instead, then where;
  • strtoupper switches Latin letters to uppercase, strtolower to lowercase;
  • ucfirst capitalizes only the first letter;
  • str_repeat('-', 5) repeats a string five times, and strrev reverses it.

The last line of output shows the main point: $text still holds its spaces. A function does not touch your string; it builds a new one and returns it. To keep the result, you assign it: $text = trim($text);.

This is where the word game is a poor hint. In the game you rearrange the tiles right on the table, while PHP functions never touch your row and lay out a new one next to it.

Searching in a string

str_contains, str_starts_with and str_ends_with

These three functions arrived in PHP 8 and answer the question "whether": they return true or false. str_contains looks for a piece anywhere, str_starts_with checks the start of the string, and str_ends_with checks the end. All three are case-sensitive: Photo and photo are different strings to them.

strpos

strpos answers a different question, "where": it returns the number of the tile where the first match starts. If there is no match, strpos returns false.

PHP
$file = 'photo.jpg';
var_dump(str_contains($file, 'Photo')); // bool(false)
var_dump(str_ends_with($file, '.jpg')); // bool(true)
var_dump(strpos($file, '.')); // int(5)
var_dump(strpos($file, 'photo')); // int(0)
var_dump(strpos($file, 'png')); // bool(false)

The dot sits on tile number 5. The word photo stands at the very start, so its position is 0. The name has no png extension, and strpos returns false.

Common mistake

The result of strpos is checked right inside an if:

PHP
if (strpos($file, 'photo')) { // ✗
    echo 'found', "\n";
} else {
    echo 'not found', "\n";
}

The program prints not found, and PHP reports nothing. The strpos call returned 0, and, as you already know, 0 counts as false in an if. The word stands at the very start, yet the check "does not find" it.

Interviewers love this trap. Before PHP 8 the result was compared with false explicitly: strpos($file, 'photo') !== false means "somewhere in it", strpos($file, 'photo') === 0 means "at the start". Today these checks use str_contains and str_starts_with.

Key points

  • strlen gives the length of a string, $s[0] the first character, $s[-1] the last one.
  • substr($s, $start, $length) cuts out a piece, and a negative start counts from the end.
  • String functions return a new string, and the variable keeps the old one until you overwrite it.
  • strpos returns a position or false, and position 0 is false in an if; str_contains answers "whether".