On each example of form processing I found this scenario:
if(!empty($_POST)) {...
... and than - so many lines (sometimes about 250) which belongs to the above if
.
Very unsuitable to track.
Is there a way like this:if(empty($_POST)) {
show show some message or echo... anything}
, and then - stop the code
So all next lines will be outside of very long if
and they'll still execute only if that if
is satisfied.
I tried something, but the members of $_POST
(username, pass...) in this case are not available.
Define a function that will execute the code necessary and pass the $_POST variable as an argument. In that way the required variables (e.g. username, password) are now available in the function
function executePost($post) {
// execute code here with $post variables
$username = $post['username'];
$password = $post['password'];
}
You call the function after you checked the post
if(!empty($_POST)) {
executePost($_POST);
} else {
echo 'Post was empty';
}
When you are inside a function you can use return
inside the if(empty($_POST)) {...}
. Otherwise you can use die(message)
or exit()
to stop the whole script execution.