PHP chop() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, chop() removes characters from the end of a string. It is an alias of rtrim().

The string is required. An optional character mask controls which trailing characters are removed.

Syntax

Call chop on the string. Start with this line.

PHP
$clean = chop($text);
CallWhat it does
chop("hello ")Removes trailing whitespace.
chop("data***", "*")Removes trailing asterisks.

Prefer rtrim() in new code for clarity. chop() does not trim the start.

Library declarations
PHP
chop(string $str, string $characters = " \n\r\t\v\x00"): string

Parameters

ParameterDescription
strRequired. The input string.
charactersOptional. Characters to strip from the end. Default is whitespace.

Return Value

Return ValueDescription
stringA new string with trailing characters removed.

PHP Examples

Example 1 — Strip Trailing Spaces

Default chop() removes trailing whitespace from the end only.

PHP
<?php
$text    = "Hello, World!   ";
$trimmed = chop($text);

echo "Before: '$text'\n";
echo "After:  '$trimmed'\n";
?>

How It Works

  1. Start with spaces. $text ends with three spaces.
  2. Call chop. Default mode strips trailing whitespace characters.
  3. Get a copy. $trimmed is clean; $text is unchanged.
  4. Middle stays. Spaces or punctuation in the middle are never removed.

Example 2 — Custom Character List

Pass a second argument to strip only those trailing characters. Leading spaces stay—unlike trim().

PHP
<?php
$text    = "   PHP is awesome   \t";
$trimmed = chop($text, " \t");

echo "Result: '$trimmed'\n";

$filename = "report.pdf...";
$clean    = chop($filename, ".");

echo "Filename: $clean\n";
?>

How It Works

  1. Limit the set. " \t" means only spaces and tabs may be removed from the end.
  2. Keep the left. Leading spaces in $text remain.
  3. Strip dots. chop($filename, ".") removes every trailing ..
  4. Stop at non-match. Removal stops at f in pdf.

Example 3 — chop() vs trim() / ltrim()

Same input, three sides of trimming. chop() matches rtrim().

PHP
<?php
$s = "  hello world  ";

echo "chop():  '" . chop($s) . "'\n";
echo "ltrim(): '" . ltrim($s) . "'\n";
echo "trim():  '" . trim($s) . "'\n";
echo "rtrim(): '" . rtrim($s) . "'\n";
?>

How It Works

  1. chop / rtrim. Right spaces gone; left spaces remain.
  2. ltrim. Left spaces gone; right spaces remain.
  3. trim. Both ends cleaned.
  4. Alias check. chop() and rtrim() print the same result.

Frequently Asked Questions

chop() removes characters from the end (right side) of a string. By default it strips whitespace characters such as spaces, tabs, and newlines. It does not remove anything from the beginning of the string.
string chop(string $string, string $characters = " \t\ \r\0\x0B"). The first argument is the input string; the optional second argument lists which characters to strip from the end.
Yes. chop() is an alias of rtrim(). They behave identically—both remove characters from the right end of a string only. Prefer rtrim() in new code for clearer naming.
chop() (like rtrim()) removes characters from the end only. trim() removes characters from both the beginning and the end. Use chop() when trailing junk is the problem; use trim() when you need both sides cleaned.
When you omit the second argument, chop() removes space, tab (\t), newline (\ ), carriage return (\r), NUL byte (\0), and vertical tab (\x0B).

Next: chr()

Convert integer code points into characters.

chr() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful