PHP chop() Function

Last Updated:

PHP chop()

The chop() function is used to remove the whitespaces and other predefined characters from the right side of a string.

It is an alias of rtrim() function.

Example

PHP Compiler
<?php $str = "John "; // without chop() echo $str . "Doe <br>"; // with chop() echo chop($str) . "Doe"; ?>

Syntax

chop(str, char_list)

Parameter Values

ValueTypeExplanation
str Required Specifies the string to be trimmed.
char_list Optional Specifies the additional character(s) to remove from the string.
Without this parameter, the function removes the following characters.
  • ' ' - An ordinary space.
  • '\0' - The NUL byte
  • '\t' - tab
  • '\n' - line feed.
  • '\x0B' - a vertical tab.
  • '\r' - a carriage return.

Return Value

ValueExplanation
string Returns an altered string.

More Example

Example

PHP Compiler
<?php $str = "John Doe"; echo chop($str, "Doe") . "<br>"; echo chop($str, "doe") . "<br>"; echo chop($str, "e") . "<br>"; ?>

Share this Page

Meet the Author