You don't need the "else" rather, only the "if".
-
<?php
-
If ($username = foo) {
-
echo 'username';
-
}
-
echo 'do something else';
-
?>
-
will return username if username = foo, and will return do something else if username = anything else. "else" or "elseif" is not required, unless there are three or more cases. The way if cases work is like this:
-
<?php
-
If (Case1) {
-
echo 'Response to case 1';
-
} elseif (Case2) {
-
echo 'Response to Case2';
-
} else {
-
echo 'response to case 3, if BOTH case1 and case2 return false';
-
}
-
echo 'continue code here';
-
?>
-
BUT, we can also have:
-
<?php
-
if (Case1){
-
echo 'Response to Case1';}
-
echo 'Response to case 2 is the continuation of the code';
-
?>
-
Or we can have:
-
<?php
-
if (Case1) {
-
echo 'Response to Case1';
-
} elseif (Case2) {
-
'Response to Case2';
-
}
-
echo 'continue the code';
-
?>
-
Finally we can have:
-
<?php
-
if (case1) {
-
echo 'response to case1';
-
} elseif {
-
echo 'response to case2;
-
} else {
-
if (Condition3) {
-
echo 'response to condition3';
-
} elseif (Condition4) {
-
echo 'response to condition4';
-
} else (Condition 5) {
-
echo 'response to condition 5';
-
}
-
}
-
echo 'continue script here';
-
?>
-
The limitation being that case2 must return false before the script will evaluate for conditions 3, 4, or 5.
The way we can make sure that the script exits after each case is to include the following as the last line of each case:
-
<?php
-
if (case1) {
-
echo 'case1 code here';
-
exit;
-
} elseif (case2) {
-
...
-
}
-
...
-
?>
-
Therefore, you would want to use something like:
-
if($full_name_length >50) {
-
echo 'error message here';
-
exit;
-
}
-
if ( !isset ($_POST['surname'] ) ) {
-
echo 'error message if user did not input a surname';
-
exit;
-
}
-
-
if (some other error condition) {
-
echo 'error message';
-
exit;
-
}
-
echo 'do code here if no errors';
-