note 51765 added to function.isset
| From: | phpnet dot 5 dot reinhold2000 at tspamgourmet dot com | Date: | Sun, 10 Apr 2005 15:33:26 +0000 |
| Subject: | note 51765 added to function.isset | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-87787@lists.php.net to get a copy of this message | ||
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')) [...]
----
Manual Page -- http://www.php.net/manual/en/function.isset.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51765
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51765&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51765&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51765&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51765&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51765&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51765&report=yes
Search -- http://master.php.net/manage/user-notes.php