Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP cannot see where my function is being redeclared

Tags:

php

I am getting this error and cannot find where the function is being redeclared.

Fatal error: Cannot redeclare gera_array() (previously declared in C:\Users\Secret\SkyDrive\Sites e Projetos\Rede Aranha\t4.php:12) in C:\Users\Secret\SkyDrive\Sites e Projetos\Rede Aranha\t4.php on line 10

Code:

function gera_ecards() {

// Funcao gera array de 5 numeros de 1 a 50 (com duplicados)                
function gera_array() {
    $ecard_rand = array();
    for ($leo = 0; $leo < 5; $leo ++) {
        $ecard_random = mt_rand(1,50);
        array_push($ecard_rand, $ecard_random);
    }
    return $ecard_rand;
}                   
// Funcao remove duplicados da array            
function array_has_dupes($ecard_rand) {
       return count($ecard_rand) !== count(array_unique($ecard_rand));
}

do {
    $ecard_rand = gera_array();
} while (array_has_dupes($ecard_rand));     
sort($ecard_rand);  
$ecard_rand = implode(",", $ecard_rand);    
return $ecard_rand; 
}

for ($leo = 0; $leo < 5; $leo ++) {
 echo gera_ecards(); 
 echo "<br>";
}
like image 586
Leandro Cintrao Avatar asked Jan 21 '26 09:01

Leandro Cintrao


1 Answers

You are declaring a function inside a function (why???):

function gera_ecards() {

  // Funcao gera array de 5 numeros de 1 a 50 (com duplicados)                
  function gera_array() {

    // ...

So every time you call your gera_ecards() function, you are declaring function gera_array() again.

You should not declare functions in functions.

like image 82
jeroen Avatar answered Jan 23 '26 23:01

jeroen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!