Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chop() vs rtrim()

Tags:

php

I'm confused with PHP functions rtrim() and chop(), as they work similar and give similar output. Why are there different functions for trimming trailing characters?

Sample examples:

PHP

$str = "Hello World!";
echo $str . "<br>";
echo rtrim($str,"World!") . "<br>"; //Hello
echo chop($str,"World!") . "<br>";  //Hello

Are there any differences between chop() and rtrim() functions?

like image 543
Sadikhasan Avatar asked Feb 13 '15 08:02

Sadikhasan


People also ask

What does the chop function do in PHP?

The chop() function removes whitespaces or other predefined characters from the right end of a string.

Which of the following is an alias for the chop () PHP function?

PHP: chop() function This function is an alias of rtrim() function.


1 Answers

The answer is also in the manual: http://php.net/manual/en/aliases.php

And a quote from there:

However there are functions which changed names because of an API cleanup or some other reason and the old names are only kept as aliases for backward compatibility.

And chop() is just a alias for rtrim() so they do the same. This is also in the manual: http://php.net/manual/en/function.chop.php

A quote from there:

This function is an alias of: rtrim().

like image 130
Rizier123 Avatar answered Sep 30 '22 06:09

Rizier123