Question

I am trying to remove the slashes from magic quotes from an array. So I have two functions, one is to remove the slashes, another is to set the variable.

// Strip slashes from an array.
function strip_magic_quotes($array)
{
    if (get_magic_quotes_gpc())
    {
        function stripslashes_array($array)
        {
            return is_array($array) ? array_map('stripslashes_array', $array) : stripslashes($array);
        }
        return stripslashes_array($array);
    }
    return $array;
}

function set_variable($array = array(),$key,$params = array())
{
    # If $params is not an array, let's make it array with one value of former $params.
    if(!is_array($params)) $params = array($params);
    if(!is_array($array)) parse_str($array, $array);

    # Strip slashes from the array if get_magic_quotes_gpc is on.
    $array = strip_magic_quotes($array);

    # This will return true or false.
    if(in_array('boolean', $params)) return isset($array[$key]) ? true : false;

    # This will regard '0' as a string.
    # Return value or 0 as a string.
    elseif(in_array('0', $params)) return isset($array[$key]) && ($array[$key] == '0') ? trim($array[$key]) : null;

    # Return null as string if 'null_to_string' is set.
    elseif(in_array('null_to_string', $params)) return isset($array[$key]) && !empty($array[$key]) ? trim($array[$key]) : 'null';

    # Check if the key is an array.
    elseif(isset($array[$key]) && !empty($array[$key]) && is_array($array[$key])) return isset($array[$key]) && !empty($array[$key]) ? $array[$key] : null;

    # This will regard '0', empty space as falsey.
    # Return value or null.
    else return isset($array[$key]) && !empty($array[$key]) ? trim($array[$key]) : null;
}

$array = array(
    'name'=>'Hello',
    'type'=>'{"page":"page"}'
);


# set the required array.
$items_variable = array(
    'name',
    'type'
);

# loop the array.
foreach( $items_variable as $item_variable )
{
    # set the main variables.
    $$item_variable = set_variable($array,$item_variable);
}

print_r($type);

I get this error in my live server which I don't understand it,

Fatal error: Cannot redeclare stripslashes_array() (previously declared in json.php:16) in json.php on line 16

line 16 refers to this line function stripslashes_array($array) which does not seem to have any mistake.

Any idea how to fix this?

Was it helpful?

Solution

What happens is that everytime you call the function strip_magic_quotes PHP tries to declare the function stripslashes_array.

Why do you think you want to nest that function? If you really want to do it this way (which you shouldn't) you can change it to:

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

Although this fixes the issue you simply shouldn't do it in the first place, because:

  • it would be better to disable (if possible) magic* for the better of mankind.
  • declaring functions in functions (although possible) should really be avoided in almost all cases.

Or you could simply just drop that inner function completely if you don't need it somewhere else and just let the code run in the outer function.

OTHER TIPS

Nested named functions are almost useless for exactly this reason.

You have to either move the nested function out or do an explicit function_exists check.

You can consider using an anonymous function in PHP 5.3 or above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top