Say I have file.php with three functions and an echo statement:
function one() {
return three() . ' This is one.';
}
function two() {
return 'This is two.';
}
function three() {
return 'This is three.';
}
echo one(); // string(xx) "This is three. This is one."
First, is it generally acceptable to have function one() call function three() even though function three() appears later in the file?
Second, when file.php is loaded in the browser (thus executing the PHP on the server), does PHP calculate the return value of function two(), even though it is never called?
Any links for further reading on how PHP process mundane things like this would be great.
Certainly. The source-order has no bearing on the order in which you call functions – it’s all parsed and available before the first line is executed.
No. It will be checked for syntax errors during parsing, but that’s it – these will be E_PARSE level errors. Other errors are only discoverable at runtime and will be E_ERROR, E_WARNING, or E_NOTICE level errors.
https://www.php.net/manual/en/errorfunc.constants.php