-2
I was here creating some loop files when I came across the following question:
Instead of calling a function that returns the die every time Ex:
<?php
if (pegar_dado('nome_dado') != '') {
echo pegar_dado('nome_dado');
} ?>
It would not be more practical and maybe better and lighter store the variable and then do the rest?
<?php
$pega1 = pegar_dado('nome_dado');
if ($pega1 != '') {
echo $pega1;
}
What I want to know is, from your experience, which of the two methods is better? Lighter? There is a difference in the use of memory in both cases?
And which of the two methods you recommend using?
Certainly the second option, imagine having to perform a function 1000 times... may be little change in the process, but it will occur, and once executed and saved in a variable, there is no need to run again.
– Wees Smith
Can you tell me if this "take data" function will be executed EVERY time I call this function? I mean, will she go through my database every time I call her? and if I store in a variable, will the database be consulted only once?
– JassRiver
If the function makes a query to the bank, yes, the query will be made every time calling the function. Calling only once will be only a query made.
– Woss
@Andersoncarloswoss If the function is called in the file several times it does the query, ok, but if the function is stored in variable and the variable is called several times, the query will be done 1 or several times?
– JassRiver
Only once. The result will be stored in memory and reused.
– Woss
It depends on the case. If the function is "pegIdSequencial" (and do what the name says) and you store the result, it will not be a sequential ID. Which to use? Depends on the intended outcome. In general, if you always want the current situation you have to call the function always, if you want the original value, even if something external changes the function result, store.
– Bacco