We used trim() to clean up a title, but the two spaces between its words are still there. Another example calls trim and seems to leave the variable unchanged. Neither observation means the function is broken. Let's separate where it removes characters from what we do with its return value.
Create experiment.php in a new folder. Replace its contents with each example in turn and run php experiment.php there. You can investigate these small cases without changing the task-list project.
Make the edges visible with brackets
<?php
$raw = " Read lesson \t\n";
$clean = trim($raw);
echo '[', $clean, ']', PHP_EOL;
var_dump($raw === $clean);
The first line is [Read lesson]. The two spaces between the words remain, while the leading spaces and trailing tab/newline disappear. \t represents a tab and \n a newline. We print the square brackets simply to show where the string begins and ends.
The following bool(false) confirms that the original and trimmed strings differ. trim() works at the edges; it does not collapse spaces inside a sentence or remove every space. We do not want to accidentally join the words in a task title.
Calling it is not the same as using its result
<?php
$title = ' Task ';
trim($title);
echo '[', $title, ']', PHP_EOL;
$title = trim($title);
echo '[', $title, ']', PHP_EOL;
The first output is [ Task ]; the second is [Task]. The first call returned a cleaned string that we ignored. The second assigned that returned string back to $title. This is another instance of the distinction we explored in the echo/return lesson.
If you need the original as well, use another variable: $clean = trim($title);. Naming the raw and cleaned values separately makes a form's data flow easier to follow.
Trim only one side
<?php
$title = ' Task ';
echo '[', ltrim($title), ']', PHP_EOL;
echo '[', rtrim($title), ']', PHP_EOL;
echo '[', trim($title), ']', PHP_EOL;
The results are [Task ], [ Task], and [Task]. ltrim() works at the start, rtrim() at the end, and trim() at both. If a line's indentation matters, for example, removing characters from both ends may be the wrong operation.
The second argument does not mean “remove this word”
<?php
echo trim('...Task...', '.'), PHP_EOL;
echo trim('happy.php', '.php'), PHP_EOL;
The first line is Task, as expected. The second is appy, not happy. The argument '.php' is not treated as one filename suffix. It specifies a set containing the dot, p, and h characters. The leading h belongs to that set too, so it gets removed.
Do not use an arbitrary trim mask to remove a filename extension or a particular word. Ordinary whitespace trimming and choosing a custom character set are different operations. Read that second argument as “these characters,” not “this exact substring.”
Not every space-looking character is the same
<?php
$title = "\u{00A0}Task\u{00A0}";
var_dump(trim($title) === $title);
The output is bool(true). Here \u{00A0} represents a nonbreaking space, not an ordinary space. Default trim leaves it in place, so the string stays unchanged. This explains one way copied text can appear to retain whitespace after trimming.
Not every application needs a rule that strips all Unicode whitespace. Decide which inputs you accept rather than assuming trim removes every invisible character. Likewise, do not alter a value such as a password without deciding whether its leading or trailing spaces are significant.
Use it in the right order for a title
<?php
$raw = $_GET['title'] ?? '';
if (!is_string($raw)) {
echo "Invalid input type";
return;
}
$title = trim($raw);
echo $title === '' ? 'Write a title' : 'Title accepted';
We check the type, trim the string, and then test whether it is empty. Without is_string(), a query parameter supplied as an array could reach trim as an unsuitable input. Trimming does not replace a type check.
Start php -S 127.0.0.1:8001 in the folder. Open http://127.0.0.1:8001/experiment.php?title=%20Task%20 and expect Title accepted. With ?title=%20%20, expect Write a title. ?title=0 is accepted because trim does not remove zero. Finally try ?title[]=Task: expect Invalid input type.
Where this appeared in the task list: PHP Lesson 03 — Read and Validate a Task Form