我知道我会为此投票,但我需要问.下面的代码是什么意思?特别,
我很困惑
我很困惑
if (empty($_POST["comment"])) { $comment = "";
这是否意味着如果用户没有插入他的评论,那么值为“$comment”的字段将为空白?如果用户已插入注释,那么值“$comment”将通过test_input函数?我看过w3schools网站,但我不是更聪明.令人难以置信的是,没有任何网站甚至描述了test_input的作用,相信我,我已经看过几十个网站.事实上,我并不是唯一认为test_input是一个神秘函数的SO用户.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo 'posted';
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
test_input是用户定义的函数,而不是PHP内置的函数.查看代码确实意味着如果没有插入注释,那么它将为空,但是如果它不为空则通过test_input函数运行注释.
在这个页面http://www.w3schools.com/php/php_form_validation.asp上,我们可以看到他们声明了test_input函数的位置,特别是页面的这一部分
<?PHP
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
让我们举例说,每个评论都是你想要在最后设置日期和时间戳,下面的代码就可以实现这一点.
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
echo 'posted';
if (empty($_POST["comment"]))
{
$comment = "";
}
else
{
$comment = add_date($_POST["comment"]);
}
}
function add_date($comment)
{
return $comment . date('d-m-Y G:i:s');
}
因此,如果用户输入Hello,则这是注释,$comment现在是Hello,这是注释28-11-2014 16:00:00
因此,为了最终确定,test_input只是由w3schools创建的函数名称,并且只要页面引用任何类型的输入验证等,它们就会使用它.