sven_raju sven_raju - 3 months ago 8
PHP Question

Is a PHP function done every time called?

Is a PHP function done on the server side each time I call it?
I am asking this because I wan't to know if it affects my performance.

Example:

<?php
function really_long_loop() {
$array = array();
for ($i=1; $i < 1000; $i++) {
array_push($array, $i);
}
}
return $array;

function FirstFunction() {
$get_loop = really_long_loop();
}

function SecondFunction() {
$get_loop = really_long_loop();
}
?>


In this example does it do the long loop twice, or just once?

Answer

Now let's run your functions. In this case, you'll be running the loop twice. That's intended, of course.

<?php
 FirstFunction();
 SecondFunction();
?>

Also, $get_loop wouldn't get anything in return because your really_long_loop() doesn't return anything.

If you had a return in your function, you could apply this return to a variable, then the variable would hold the value, and the loop wouldn't be executed again when you call this variable.