Backend Development 3 min read

PHP trim() Function: Description, Syntax, Parameters, Return Value, and Examples

This article explains PHP's built-in trim() function, covering its purpose, related ltrim() and rtrim() functions, syntax, parameters, default character list, return value, and provides two practical code examples along with links to further learning resources.

php中文网 Courses
php中文网 Courses
php中文网 Courses
PHP trim() Function: Description, Syntax, Parameters, Return Value, and Examples

The trim() function is a built-in PHP function that removes whitespace and predefined characters from both ends of a string.

Related Functions

ltrim() : removes characters from the left side of a string. rtrim() : removes characters from the right side of a string.

Syntax

<code>trim($string, $charlist)</code>

Parameters

$string (required): the input string.

$charlist (optional): a list of characters to strip. If omitted, the function removes the following characters: \0 (NULL), \t (tab), \n (newline), \x0B (vertical tab), \r (carriage return), and ordinary whitespace.

Return value : the trimmed string.

Usage Examples

Example 1

<code>&lt;?php
$str = "Hello World!";
echo trim($str, "Hell!");
?&gt;</code>

Output:

<code>o World</code>

Example 2

<code>&lt;?php
header("content-type:text/html;charset=utf-8");
$str = "  欢迎来到php中文网!  ";
echo trim($str);
?&gt;</code>

Output:

<code>欢迎来到php中文网!</code>

Learning Recommendations

Links to tutorials on Vue3+Laravel8+Uniapp, Vue3+TP6 API social e‑commerce development, Swoole, and Workerman+TP6 real‑time chat systems are provided.

backendPHPstringfunctionTRIMExample
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.