This may sound like a dumb question, but does assigning a variable to a function mean it only calls the function ONE time, and can be reused unlimited times without making an additional call to the function or does it still call the function multiple times?
is this:
$variable = function_name();
echo $variable;
echo $variable;
the same as this:
echo function_name();
echo function_name();
I understand both situations are ultimately displaying the same bit of information, but I'm curious if that bit of data is fetched one time and reused many, or if it's fetched upon each echo.
So does assigning a variable to a function mean function_name();
does the work only ONE time and the variable just recycles the data for later usage or does function_name();
actually do the work TWO times?
Is it better practice to assign variables or just directly call the function each time it needs to be used?
a function should always return some values so for example, if you have a function like this.
function foo() {
return 'hello world';
}
and if you call it and assign it to a variable then
/*
* in the below example the function will be called once
* and the function's return value will be assigned to $a variable
* now $a will contain value 'hello world';
*/
$a = foo();
if you try printing the variable $a
it will simply print the value it contains and not call the function once again, since $a
now contains 'hello world';
by echoing $a variable multiple times.
echo $a;
echo $a;
echo $a;
will simply print the value hello world
three times and not call the function three times.
but when you call the function multiple times for example.
$a = foo();
$a = foo();
now coming to your question.
this will result in the function being called multiple times. and the value being overwritten from the previous value returned by the function call. So does assigning a variable to a function mean function_name(); does the work only ONE time and the variable just recycles the data for later usage or does function_name(); actually do the work TWO times?
i hope i have explained this in full details.
Is it better practice to assign variables or just directly call the function each time it needs to be used?
well it depends on the context, and the function you want to use, the bottom line is, always try returning some values from the function, for example.
a) if the function is meant for fetching some values and formatting it, then should return it as array, string or whatever data type.
b) if the function is meant to do some execution and not any fetching, then you can return boolean values like return true|false
indicating wether the operation was successful.
The function will run as many times as you call it. In the second example the function will be run twice which is inefficient if the function returns the exact same code in both cases. Caching will help some but it might get thrown out of cache, cache may have gotten full. It would be best to put it in a variable like in your first example and just output it as needed.
It will run only once
<?php
function test(){
return time();
}
$a = test();
echo $a . "
";
sleep(1);
echo $a . "
";
larikov@mb:~# php -f t.php
1343159819
1343159819
A programming language normally is pretty straight foward. You command a computer / processor to do what to do, even with high level languages like PHP.
So if you command echo "day";
it will print out the string day - like written. And like said: Pretty straight forward.
So what about calling a function:
printf("%s", 'day');
prints out the string day as well. Also does format the string 'day'
as second parameter to printf
. So the function is invoked.
Is it better practice to assign variables or just directly call the function each time it needs to be used?
That is quite a philosophical question. Technically both can be the same. If both are the same, it is more wise to call the function once, assign the return value to a valvue and store it for later use.
The benefit to storing to a variable is, that normally, this needs to be done anyway (even if not explicitly to a variable, but to memory), to it's most often useful as a programmer to think: A variable, cheap. Take it. Makes your software fast - even if you think: Uh, at least this is an additional variable this will slow things down. Truth is: Variables speed up your development generally. Additionally, PHP has copy on write, which means that copying data is actually not taking place most times.