PHP chop | Remove Characters from End of String

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.

Related Articles:

Syntax

chop($string, $characterList)

Parameters

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

Return Value

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.

Chopping Vegetables from Right End | PHP chop Function
You can remember this function by imagining that vegetables are usually chopped from the right end.

Example

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.

Conclusion

In conclusion, we discussed the PHP chop Function. You can read more about it on PHP Official Documentation. Additionally, you can learn about more PHP String Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *