What it is and why to use it
Checking whether a command begins with a known marker or a label ends in a suffix should not require a complicated expression. PHP 8 adds str_starts_with() and str_ends_with(), which return booleans directly. Their names make it clear whether the start or the end is being tested.
How it works
Each function receives the complete string followed by the substring to look for. Matching is case-sensitive and binary-safe. An empty substring matches every string, including an empty one, so reject an empty configured marker if your application requires a meaningful boundary. Neither operation parses a URL, checks file contents or performs case folding.
Common mistakes and limitations
A filename ending in .jpg does not prove that an uploaded file is an image. A text prefix does not establish URL safety or hostname ownership. Use these helpers for text rules and use dedicated validation for files, URLs and security decisions.
Run the examples locally
Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.
The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.
These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.
Check your interpreter, lint and run one saved example
php -v
php -l example.php
php example.phpContinue learning
Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.
Syntax
$hasPrefix = str_starts_with($text, 'note:');
$hasSuffix = str_ends_with($text, '.txt');Examples
Classify a small command string
Use explicit boundary checks for a trusted text format.
<?php
declare(strict_types=1);
$command = 'note:read arrays';
echo str_starts_with($command, 'note:') ? 'Note command' : 'Other command', PHP_EOL;
echo str_starts_with($command, 'NOTE:') ? 'Uppercase match' : 'Case differs', PHP_EOL;
- The first call matches the exact lowercase prefix at position zero.
- The second uses uppercase letters, so its condition is false.
- The ternary expressions choose readable labels for the boolean results. A small command parser could dispatch recognised prefixes to different handlers.
- Expected output: Note command Case differs
Filter a known list of document names
Select text labels by suffix without treating that suffix as a security guarantee.
<?php
declare(strict_types=1);
$names = ['notes.txt', 'guide.md', 'tasks.txt'];
$textFiles = array_filter($names, fn(string $name): bool => str_ends_with($name, '.txt'));
echo implode(', ', $textFiles), PHP_EOL;
echo str_ends_with('notes.txt', '') ? 'Empty suffix matches' : 'No match', PHP_EOL;
- array_filter keeps names for which the callback’s suffix test returns true.
- implode joins the selected values; it does not need consecutive numeric keys.
- The last line makes the empty-substring rule explicit. These names are fixed labels, not uploaded files whose contents have been verified.
- Expected output: notes.txt, tasks.txt Empty suffix matches
Best practices
- Decide whether case-sensitive matching fits your data.
- Reject empty configured markers when an actual prefix is required.
Runtime & versions
PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
PHP Manual