15.11.2016, 21:16
By returning a value in an if statement (and anywhere else in a function really), you:
- terminate the function that the if-statement is in (when the if-statement is executed);
- can treat everything outside that if statement as the else statement; i.e:
PHP код:someFunction() {
if(someCondition)
// Terminate the function
return true;
// code under this is ONLY executed when that if statement evaluates to false, thus it can be considered the 'else' statement for that if statement.
return false;
}
PHP код:someFunction() {
if(someCondition)
// Terminate the function
return true;
// code
return true; // return value is true which is the same as in the if block
}
PHP код:someFunction() {
if(someCondition) {
// code
}
else {
// code
}
return true;
}