note 51765 deleted from function.isset by cmb
| From: | cmb@php.net | Date: | Sun, 10 Feb 2019 11:23:39 +0000 |
| Subject: | note 51765 deleted from function.isset by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210213@lists.php.net to get a copy of this message | ||
Note Submitter: phpnet dot 5 dot reinhold2000 at t spamgourmet dot com
----
if you want to check whether the user has sent post vars from a form, it is a pain to write
something like the following, since isset() does not check for zero-length strings:
if(isset($form_name) && $form_name != '') [...]
a shorter way would be this one:
if($form_name && $form_message) [...]
but this is dirty since you cannot make sure these variables exist and php will echo a warning if
you refer to a non-existing variable like this. plus, a string containing "0" will
evaluate to FALSE if casted to a boolean.
this function will check one or more form values if they are set and do not contain an empty string.
it returns false on the first empty or non-existing post var.
<?
function postvars() {
foreach(func_get_args() as $var) {
if(!isset($_POST[$var]) || $_POST[$var] === '') return false;
}
return true;
}
?>
example: if(postvars('form_name','form_message')) [...]