Regarding PHP functions, if you do not need a function to return a BOOLEAN or STRING, is there a difference between declaring a return on failure of a condition, rather than just letting the function automatically return?
For example,
Is there any internal difference between:
function check() {
if( 5 > $v ) {
die('yes');
}
}
function check() {
if( 5 > $v ) {
die('yes');
}
else {
return;
}
}
Obviously, they appear to do the same exact thing on failure of the ‘IF’ condition, but internally, is one better than the other in for sake of memory, security, usability, or overall best practice?
There is no effective, real-world difference in these statements in terms of memory or security. None, nada, zip, zero. They are completely identical. The few bytes needed to hold the additional opcodes is irrelevant, and if you care about the time spent parsing, tokenizing and interpreting the opcodes, you are engaging in micro-optimization.
In terms of “usability”, the former is more clear by far. Returning from the
elseis silly. If I saw that in live code, I’d assume that whoever wrote it was distracted when doing so and clean it up.