PHP 函数 “test_input()” 到底是做什么的?


What exactly does the PHP function "test_input()" do?

我知道我会因此被投票否决,但我需要问。下面的代码是什么意思?具体说来我很困惑

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 this is a comment$comment现在将被Hello this is a comment 28-11-2014 16:00:00

因此,最后确定一下,test_input只是w3schools创建的函数名称,每当页面引用任何类型的输入验证等时,他们都会使用它。

test_input()是w3schools使用的用户定义函数:

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

例如:

<?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;
  }
?>
  • trim($data)将从用户输入数据(使用 PHP trim() 函数)中删除不必要的字符(额外的空格、制表符、换行符)。
  • stripslashes($data)将从用户输入数据中删除反斜杠()(使用PHP stripslashes()函数)。
  • htmlspecialchars($data)将特殊字符转换为 HTML 实体。
    • (如果用户输入 <> ,htmlspecialchars() 会将其转换为 &lt;&gt; )。

没有任何像 test_input() 这样的 PHP 函数。

它可能是w3schools网站中的用户定义函数。只需访问您提到的网站并运行示例代码,然后在tryit编辑器中检查一次源代码,您就可以在那里获得该功能。

有时网站在给出解释时不会显示所有功能,因此只需参考源代码

test_input()不是

标准的PHP函数,但你知道你可以像这样制作自己的函数:

function test_input($input){
   $output = $input." Oh, I'm updated."; // Whatever process you want to make
   return $output;
}

然后,您可以在 php 脚本中调用test_input()

所以基本上你应该问作者这是做什么的,或者只是搜索test_input()的定义。

如果您使用框架,库或插件,则定义了大量非标准函数,您可以检查文档(api)或阅读源代码以找出答案。

也许

你可以发布整个脚本,也许我们可以找出test_input()在哪里。