sheno
sheno

Reputation: 273

php function cannot be called for the second time

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?

Upvotes: 0

Views: 654

Answers (7)

Jessica
Jessica

Reputation: 7005

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)

Upvotes: 2

VladH
VladH

Reputation: 393

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

Upvotes: 0

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174957

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.

Upvotes: 0

Rezigned
Rezigned

Reputation: 4932

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

Upvotes: 0

som
som

Reputation: 4656

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.

Upvotes: 0

Evert
Evert

Reputation: 99533

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);
}

Upvotes: 0

Anigel
Anigel

Reputation: 3437

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");
}

Upvotes: 2

Related Questions