What it is and why to use it
A pasted username or a line read from a text file may contain surrounding spaces or line endings. trim() returns a string with selected characters removed from both ends. It does not collapse internal whitespace. This makes it useful as one small normalisation step before validating fields whose rules permit trimming.
How it works
Without a second argument, trim removes ordinary space, tab, line feed, carriage return, NUL and vertical tab. A custom second argument is a set of individual characters, with optional ranges, rather than a prefix or suffix substring. ltrim and rtrim restrict removal to one end. The byte-based defaults do not cover every Unicode whitespace character.
Common mistakes and limitations
Do not automatically trim passwords or text where leading spaces are meaningful. Do not expect trim($text, "draft") to remove exactly the word draft: it removes any listed character at either edge. Trimming alone does not make input valid or safe to insert into HTML.
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
$clean = trim($text);Examples
Remove surrounding spaces without changing a name
Normalise a simple display-name field while keeping its internal spacing.
<?php
declare(strict_types=1);
$input = ' John James ';
$clean = trim($input);
echo '[', $clean, ']', PHP_EOL;
echo strlen($clean), PHP_EOL;
- trim removes the spaces at the two boundaries and leaves the one between the names.
- Square brackets make the absence of boundary spaces visible in the output.
- strlen counts bytes; the fixed ASCII sample has ten bytes. For user-facing Unicode character counts, use the appropriate multibyte or grapheme function.
- Expected output: [John James] 10
Use a deliberate character mask
Strip decorative separators from the ends of a label.
<?php
declare(strict_types=1);
$label = '---PHP---';
echo trim($label, '-'), PHP_EOL;
echo rtrim('PHP///', '/'), PHP_EOL;
- The custom mask contains one character, so trim repeatedly removes hyphens from either edge.
- rtrim removes forward slashes only from the right-hand edge of the second sample.
- Neither function understands URL structure or file-path security. These are controlled formatting operations, not path validators.
- Expected output: PHP PHP
Best practices
- Normalise only fields whose rules allow the change.
- Treat custom trim masks as character sets, not substrings.
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