Home  >  Article  >  Backend Development  >  PHP does not allow users to submit empty forms (php empty value judgment)_PHP tutorial

PHP does not allow users to submit empty forms (php empty value judgment)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:25:121052browse

You can modify the code and add some judgment:

Copy code The code is as follows:

if(empty($_POST['name'])){
echo "As the saying goes, when a wild goose passes by, it leaves behind a voice and a person leaves behind a name
";
}

elseif(empty($_POST['comment'])){
echo "Hey, please say a few more words~";
}
else{
$sql = "INSERT INTO myblog_comments(blog_id, dateposted, name, comment) VALUES(" . $validentry . ", NOW(), '" . $_POST['name'] . "', '" . $_POST['comment'] . " ');";
mysql_query($sql);
header("Location: http://". $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']."?id=" . $validentry);
}

PHP null value judgment

Although empty and isset are both variable processing functions, they are both used to determine whether the variable has been configured, but they have certain differences: empty will also detect whether the variable is empty or zero. When a variable value is 0, empty considers the variable to be equivalent to being empty, which is equivalent to not being set.

Copy code The code is as follows:

/*For example, to detect the $id variable, when $id =0, use empty and isset to detect whether the variable $id has been configured. Both will return different values ​​- empty thinks there is no configuration, isset can get the value of $id: */
$id=0;
empty($id)?print "It's empty .":print "It's $id .";
//Result: It's empty .
print "
";
!isset( $id)?print "It's empty .":print "It's $id .";
//Result: It's 0 .
?>

To summarize, "NULL" and "empty" are two concepts in PHP.

isset is mainly used to determine whether a variable has been initialized.
empty can judge variables with values ​​of "false", "empty", "0", "NULL" and "uninitialized" as TRUE
is_null Only variables with a value of "NULL" are judged as TRUE
var == null Judge variables with a value of "false", "empty", "0", and "NULL" as TRUE
var === null Only variables with a value of "NULL" are judged as TRUE
So when we judge whether a variable is really "NULL", we mostly use is_null to avoid "false", "0" and other values. interference.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825197.htmlTechArticleYou can modify the code and add some judgments: Copy the code as follows: if(empty($_POST['name'] )){ echo "As the saying goes, when the geese pass by, the people who leave their names will leave their namesbr /"; } elseif(empty($_POST['comment'...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn