0

This code is self-explanatory. After I call the function and it works fine, other calls would fail:

<?php

function htmlFilter_array(&$html_array)
{
    function nested_clean(&$value) 
    {
      $value = htmlentities($value, ENT_QUOTES, "UTF-8");
    }
    array_walk_recursive($html_array, 'nested_clean');
}

$arr1=array("id"=>"1");
echo "line 1 <br/>";
$arr2=array("id"=>"2");
echo "line 2 <br/>";
$arr3=array("id"=>"3");
echo "line 3 <br/>";
htmlFilter_array($arr1);
echo "line 4 <br/>";
htmlFilter_array($arr2);
echo "line 5 <br/>";
htmlFilter_array($arr3);
echo "line 6 <br/>";

?>

this is the result:

line 1
line 2
line 3
line 4 

why line 5 and 6 cannot run?

sheno
  • 273
  • 1
  • 5
  • 16

7 Answers7

2

Really you should show errors and warnings whilst developing code. it would tell you what the problem is.

E_ERROR : type 1 -- Cannot redeclare nested_clean() (previously declared in main/code_145461.php:5) -- at line 5

You are redeclaring the function by nesting the function definition inside the other function.

I am not sure why you would nest your functions like this.

try

function htmlFilter_array(&$html_array)
{

    array_walk_recursive($html_array, 'nested_clean');
}

function nested_clean(&$value) 
{
  $value = htmlentities($value, ENT_QUOTES, "UTF-8");
}
Anigel
  • 3,435
  • 1
  • 17
  • 23
2

If you don't want the function to be accessible outside of your other function, you can use an anonymous function. http://php.net/manual/en/functions.anonymous.php (AKA closure)

Jessica
  • 7,075
  • 28
  • 39
0

First: TURN ON ERROR REPORTING. You cannot program if PHP does not tell you when something is broken.

Answer to your question: you can only define a function once. The second time it breaks because you're trying to redefine function. Easiest is to not nest the function, PHP doesn't allow you to nest functions like that anyway (it's all in the global scope).

Alternative fix:

function htmlFilter_array(&$html_array)
{
    $nested_clean = function(&$value) 
    {
      $value = htmlentities($value, ENT_QUOTES, "UTF-8");
    };
    array_walk_recursive($html_array, $nested_clean);
}
Evert
  • 93,428
  • 18
  • 118
  • 189
  • I can explain all the required php.ini settings, or you can google it and find it yourself in 10 seconds :) you can do it! – Evert Jun 27 '13 at 13:10
  • hmm, i was using `error_reporting(E_ALL)` which did not work. – sheno Jun 27 '13 at 13:16
  • display_errors needs to be on too, and you should fix it in php.ini and not in your script. – Evert Jun 27 '13 at 13:22
0
function nested_clean(&$value)
{
    $value = htmlentities($value, ENT_QUOTES, "UTF-8");
}

function htmlFilter_array(&$html_array)
{
    array_walk_recursive($html_array, 'nested_clean');
}

$arr1=array("id"=>"'1");
echo "line 1 <br/>";
$arr2=array("id"=>"'2");
echo "line 2 <br/>";
$arr3=array("id"=>"'3");
echo "line 3 <br/>";
htmlFilter_array($arr1);
echo "line 4 <br/>";
htmlFilter_array($arr2);
echo "line 5 <br/>";
htmlFilter_array($arr3);
echo "line 6 <br/>";

print_r( $arr1 );

Why are you not using like above code?

Otherwise you can use Closure function.

som
  • 4,650
  • 2
  • 21
  • 36
0

I think you should turn on error reporting first. But from the code I'm 100% sure that line 5-6 produce no output because in htmlFilter_array it will show some error like cannot redeclare function nested_clean

Rezigned
  • 4,901
  • 1
  • 20
  • 18
0

The problem is you using nest functions, the function gets redeclared over and over again.

function nested_clean(&$value) 
{
  $value = htmlentities($value, ENT_QUOTES, "UTF-8");
}

function htmlFilter_array(&$html_array)
{
    array_walk_recursive($html_array, 'nested_clean');
}

The solution is declaring the function outside, even though you're calling it repeatedly.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

Try to separate the functions and call nested_clean inside htmlFilter_array... there is no use of declaring it every time you call htmlFiler_array

VladH
  • 383
  • 4
  • 16