stripslashes() Function Use in PHP 8.4 and 8.5

The stripslashes() function in PHP 8.4 and 8.5 works fundamentally the same as in previous versions, but there is one critical change in PHP 8.5 regarding how it handles null values.

Function

stripslashes() un-quotes a quoted string by removing backslashes. It is primarily used to clean up data that was automatically escaped by legacy features like magic_quotes_gpc (removed in PHP 5.4) or when you simply want to display raw form input without escaping .

  • Signature: stripslashes(string $string): string
  • Behavior: It converts to and \ to . It is notrecursive, so it will not process arrays automatically .

Example

<?php
$str = "Is your name O reilly?";

// Outputs: Is your name O reilly?
echo stripslashes($str);
?>

PHP 8.5 Specific Change: Null Deprecation

The most significant update for PHP 8.5 is the deprecation of passing null to the $string parameter. In PHP 8.4, this might have worked silently, but in PHP 8.5, it will trigger a deprecation warning.

If your code receives data (like from $_REQUEST) that might be null, you must handle it before calling the function .

Incorrect (PHP 8.5 Deprecation):

// Triggers: Deprecated: stripslashes(): Passing null to parameter #1...

$value = stripslashes($_REQUEST[ param ]);

Correct (PHP 8.4 & 8.5 Compatible):

// Use the null coalescing operator to default to an empty string

$value = stripslashes($_REQUEST[ param ] ?? );

Handling Arrays (Recursive Example)

Since the function only works on strings, you need a custom recursive function to clean multi-dimensional arrays (like $_POST or $_GET).

<?php
function stripslashes_deep($value)
{
   $value = is_array($value) ?
               array_map( istripslashes_deep , $value) :
               stripslashes($value);

   return $value;
}

// Example Usage
$array = array("f\ oo", "b\ ar", array("fo\ o", "b\ ar"));
$array = stripslashes_deep($array);

print_r($array);
/*
Output:
Array
(
   [0] => f oo
   [1] => b ar
   [2] => Array
       (
           [0] => fo o
           [1] => b ar
       )
)
*/
?>

9 Views