W3docs

chop()

Our article is about the PHP function chop(), which is used to remove whitespace or other characters from the end of a string. This function is useful for

The PHP chop() function removes whitespace (or other specified characters) from the end (right side) of a string and returns the result. It does not touch the start of the string and never changes the original variable — it returns a new string.

This page covers the syntax of chop(), which characters it strips by default, how to pass a custom character mask, and the common gotchas to watch for.

chop() is an alias of rtrim()

The most important thing to know: chop() is simply an alias of rtrim(). The two functions are identical in behavior — chop is the older name (familiar to Perl developers), while rtrim ("right trim") is the more descriptive one. New code generally prefers rtrim(), but you will still see chop() in existing codebases.

If you need to strip from the left side, use ltrim(); to strip from both sides, use trim().

Syntax

chop(string $string, string $characters = " \n\r\t\v\0"): string

The function takes one or two parameters:

  • $string — the string to trim.
  • $characters (optional) — a list of characters to strip from the end. When omitted, chop() removes the default whitespace characters listed below.

It returns the trimmed string. The input is passed by value, so $string itself is left unchanged.

Default characters stripped

When you call chop() with one argument, it removes these characters from the end:

CharacterMeaning
" "ordinary space
"\t"tab
"\n"newline (line feed)
"\r"carriage return
"\0"NUL byte
"\v"vertical tab

Removing trailing whitespace

The most common use is cleaning trailing spaces, tabs, or line breaks — for example, after reading a line from a file:

php— editable, runs on the server

We append a | so the trimmed boundary is visible. Output:

Hello, World!|

The trailing spaces are gone, while everything before them is untouched.

Using a character mask

Pass a second argument to strip a custom set of characters instead of whitespace. Every character in the mask is removed from the end, repeatedly, until a character that is not in the mask is reached:

php— editable, runs on the server

Output:

Hello, Wor

The mask "ld!" is a set of characters, not a substring. Reading from the right, !, d, l, then d are all in the set and get removed; r is not in the set, so trimming stops there.

Character ranges with ..

Inside the mask you can express a range with two dots. For example, "0..9" means "any digit", so this strips all trailing numbers:

<?php
echo chop("order100200", "0..9");
?>

Output:

order

Common gotchas

  • It only trims the ends, not the middle. chop("a b ") returns "a b" — the spaces between a and b stay.
  • The mask is a character set, not a literal suffix. chop("Hello, World!", "Hd") returns the string unchanged, because the last character ! is not in the set H/d, so trimming never starts.
  • It does not modify the original variable. Always assign or use the return value: $clean = chop($raw);.

When to use it

Reach for chop() / rtrim() whenever trailing characters would break a comparison or display:

  • Removing the newline at the end of a line read with fgets().
  • Cleaning up user input before storing it.
  • Stripping a trailing slash from a path or URL: rtrim($path, "/").

For replacing characters anywhere in a string (not just the ends), use str_replace() instead.

Practice

Practice
What is the function of the 'chop()' function in PHP?
What is the function of the 'chop()' function in PHP?
Was this page helpful?