PHP功能究竟是什么" test_input()"做?

时间:2014-11-28 06:53:04

标签: php sql

我知道我会因此而被投票,但我需要问一下。下面的代码是什么意思?特别,

我很困惑
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"]);
    }

4 个答案:

答案 0 :(得分:10)

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创建的函数名称,只要页面引用任何类型的输入验证等,它们就会使用它。

答案 1 :(得分:8)

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;)。

答案 2 :(得分:2)

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()的位置。

答案 3 :(得分:0)

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

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

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