Chop () vs rtrim ()

I got confused with the PHP functions rtrim() and chop() as they work the same and give similar output. Why are there various functions for trimming trailing characters?

Examples of 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() ?

+5
source share
4 answers

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

And a quote from there:

However, there are functions that change their names due to cleaning APIs or for some other reason, and old names are saved only as aliases for backward compatibility .

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

Quote from there:

This function is an alias: rtrim ().

+6
source

How can you read the documentation

 chop — Alias of rtrim() 
+4
source

For historical reasons, PHP has some Functions that do the same thing.

Some of them are outdated. Some of them still exist.

split and preg_split are another example, although they both work a little differently (this is not the case if the function is an alias for the other)

+1
source

Others answered why there are two functions that perform the same thing.

However :

Is there a difference between chop and rtrim?

Yes, there is a difference!

In terms of functionality there is (currently) no difference. They both (currently) work the same way as one, it's just an alias of the other.

However , notice that PHP.net still states :

It is usually a bad idea to use these aliases, as they may be related to deprecation or renaming, which will lead to an unmanaged script.

The difference is that this is an older function, reserved for backward compatibility.
This is not pedantic. As with any changes in PHP (e.g. mysql_ functions, depreciable), if you have a choice, you should use the very latest.

Perhaps the alias may be removed in a future version of PHP, or the alias is no longer supported, and therefore you lose the improvements made to the new function.

+1
source

Source: https://habr.com/ru/post/1213302/


All Articles