PHP chop is an inbuilt function in PHP. It removes whitespaces (or other characters) from the end of a string. In this article, we will discuss the PHP chop Function.
Also, this function is an alias of PHP rtrim Function.
- PHP trim Function: Remove Characters from both Left and Right Side of String.
- PHP ltrim Function: Remove Characters only from Beginning of String.
The PHP chop Function expects two parameters. However, only one parameter is mandatory and the other is optional.
- $string: The first parameter to the function is the string to operate on. It is a mandatory parameter.
- $characterList: The second parameter to the function is an optional parameter. It specifies the character list to remove from the string. By default, the function removes all the following characters:
- “\0” – NULL
- “\t” – tab
- “\n” – new line
- “\x0B” – vertical tab
- “\r” – carriage return
- ” ” – ordinary white space
The chop function in PHP returns the string after removing whitespaces and all other predefined characters from the right side (end of the string).
Note: The function returns an empty string when the argument is a null variable.
For instance, consider a simple string containing whitespaces on both ends.
<?php $string = ' Simple string '; $trimmedString = chop($string); var_dump($trimmedString); ?>
In the above example, the function chops whitespaces only from the right end. The spaces from on the left side remain as it is.
OUTPUT: string(15) " Simple string"
For more detailed examples, you can refer to PHP rtrim Function.
Vishesh is currently working as a Lead Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.