Conditionals and fuctions

if you have a COMPOUND conditional statement ( such as “IF”) that based on the return value of a function… FOR EXAMPLE:
if ( foo()>0 && foo()%2==1 && (foo()==7 || foo()!=2){…} // the value returned by foo() is not used anywhere else is the function called multiple times or is the value cached by PHP?

I guess what I am asking is … tho it would involve an ADDITIONAL LINE of code and memory allocation , in situations as described is better to store the value returned foo() in a variable or are there NO significant gains ( other than organizational) ?

PHP won’t cache it.

Performance wise, I doubt anyone here knows the exact difference between say… 4 function calls vs 1 function call + a variable. However function calls do have a slight overhead, but so does initializing and assigning a value to a variable. Obviously it would depend heavily on what the function is doing as well.

Generally this performance issue is apparent in loops, like for loops where you might do something like for ($i=0; $i < foo(); $i++) and it’ll be run thousands of times.

However in your case of just a handful of times, I would personally say - use a variable, because from a maintenance perspective - it will be easier to modify. Performance wise it won’t matter either way.

Without knowing the steps function foo() does…can only guess if it would benefit being saved to a variable.

if ( ( $bar = foo() ) > 0 && ( $bar % 2 ) == 1 && ( $bar == 7 || $bar != 2 ) ) { ... }