Search code examples
phpstripslashes

Why check if stripslashes function exists?


I've found the following code that checks if the stripslashes() function exists.

if ( function_exists( 'stripslashes' ) ) {
    // Do something
} else {
    // Do something
}

The stripslashes() function works on PHP4 and PHP5, so I wonder why it needs a conditional statement to check if the function exists. I don't get it.

It's not a subjective question. Just tell me what is the difference between the usage of this statement and not using it. Thanks in advance!

Here are related links as to where they were used:


Solution

  • There used to be a feature in PHP known as magic quotes, which while well-intentioned, has caused endless confusion.

    Most likely this code is intended to detect magic quotes, however this is not the correct way to do this, especially since it does not work.

    The correct way to detect if magic quotes are enabled is to use the fuction made for the purpoes, get_magic_quotes_gpc like so.

    if (get_magic_quotes_gpc()) {
    

    Or perhaps the following, if you are concerned this will be removed.

    if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
    

    That being said, the whole magic quotes feature was removed back in PHP 5.4, so unless you need to support obsolete versions of PHP, you can just forget the whole thing ever existed (unless you use WordPress that is...).

    On a side note, I suppose it's possible the stripslashes function may be removed in the future, and may not have existed at one point, but in this context that's probably not the reason.