Repeat For Error Function

Asked

Viewed 88 times

0

I have an error inside a for loop. The Error gives me to the Function line.

$Valor = "SELECT max(id) FROM tb_empresa";
For ($contatador = 0; $contador = $Valor; $contador++){

//Código 

function estaParaExpirar($data, $dias=10) {
if (!strtotime($data) || empty($data)) return false;
return(strtotime($data) < strtotime("+".$dias. "days") );
}

Fatal error: Cannot redeclare estaParaExpirar()

  • 3

    Okay, let’s go in parts: $contactor is spelled wrong. It’s $counter, and $counter = $Value should be $counter == $Value. If the function is Open() has its scope inside the loop, I recommend to put it outside and use only the call to it inside the is.

  • 3

    Just to explain what @Dalton quoted: $contador = 0 you assign a value to the counter; $contador == 0 checks a simple equality where 0(integer) is equal to '0' (string); $contador === 0 checks an exact match in relation to the type where 0(integer) is different from '0' (string)

  • 4

    On the question, you NAY can declare more than one function with the same name Cannot redeclare. What would be the logic of having 2 identical functions?

1 answer

2


It gives error because you set the function estaParaExperience within a loop, which causes each loop cycle and function to be declared and a function can only be declared once within its scope. Here are two options: declares the function outside the loop and calls inside the loop , or removes the function statement and executes the function code inside the loop

$Valor = "SELECT max(id) FROM tb_empresa";
For ($contatador = 0; $contador == $Valor; $contador++){

//Código 

if (!strtotime($data) || empty($data)) 
    return  false;

return (strtotime($data) < strtotime("+".$dias. "days") );


}
  • Your return will return TRUE|FALSE, but what is the purpose of a Return inside a for outside a function scope?

  • My basis is the code that was presented. The purpose of Return within "for" is to stop the script when a given condition is reached and return that value. Note that I said interrupt the "script". I don’t know if that’s the purpose, I just didn’t change much what was presented

  • View control break.

  • The break control stops the cycle but does not stop the script, nor does it return any results. You can also use continue if you want to skip any interaction. It depends on what you want to do. I, in this example, chose to make the script return a result. I could also have chosen to assign this result to a variable.

  • devolvesse um resultado. Returns the result to nowhere, as no variable will assign the value of for.

  • Both a script and a function can return a value. The script returns a value to the client, which in this case can be an ajax request.

  • 1

    I am in the cell but I will comment. Request for ajax is echo and not Return, besides, the question does not make any reference to javascript

  • I was just testing this, using Return in the global scope interrupts script execution but does not return the value of return for the client, therefore it cannot be obtained as a result of an ajax request, for this it is necessary to use echo followed by exit or directly exit(resultado)

  • Okay, I wasn’t as precise as I should have been. The initial idea was to explain the concept and not to change the code too much. What I understood from the question was that when there was a certain condition stop the script and do anything with that condition. I assumed that it could be the method of an object and that this method would return a value. We are dealing with concepts, abstract things. Actually Return within a global scope usually prints nothing.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.