PHP 函数“test_input()”究竟是做什么的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27183416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:13:27  来源:igfitidea点击:

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

phpsql

提问by user3972671

I know I will be voted down for this, but I need to ask. What does the code below mean? Specifically, I am confused by

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

if (empty($_POST["comment"])) { $comment = ""; 

Does this mean that if the user has not inserted his comments, then the field for which the value is "$comment" will be blank? And if the user HAS inserted a comment, then the value "$comment" will be put through the test_input function? I have looked at the w3schools website but I am none the wiser. It is unbelievable that NO website even describes what test_input does, and believe me, I've looked at dozens of websites. In fact, I am not the only SO user who thinks that test_input is a mysterious function.

这是否意味着如果用户没有插入他的评论,那么值为“$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"]);
    }

回答by The Humble Rat

test_inputis a user defined function and not a function built into PHP. Looking at the code it does indeed mean that if no comment has been inserted then it will be empty, however if it is not empty run the comment through the test_input function.

test_input是用户定义的函数,而不是 PHP 内置的函数。查看代码确实意味着如果没有插入注释,那么它将是空的,但是如果它不是空的,则通过 test_input 函数运行注释。

On this page http://www.w3schools.com/php/php_form_validation.aspwe can see where they have declared the test_inputfunction, specifically this part of the page

在这个页面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;
}
?>

Lets say for example that every comment that was made you wanted to put a date and timestamp on the end, the following code would achieve this.

例如,假设您希望在最后添加日期和时间戳的每条评论,以下代码将实现这一点。

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');
 }

Therefore if the user entered Hello this is a comment, $commentwould now be Hello this is a comment 28-11-2014 16:00:00

因此,如果用户输入Hello this is a comment$comment现在将是Hello this is a comment 28-11-2014 16:00:00

So to finalize, test_inputis simply a function name created by w3schools and they use it whenever the page refers to any kind of validation of input etc.

所以最终确定,test_input只是一个由 w3schools 创建的函数名称,每当页面涉及任何类型的输入验证等时,他们都会使用它。

回答by Lloyd Dominic

test_input()is a user - defined function used by w3schools:

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

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

For example:

例如:

<?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)will strip unnecessary characters (extra space, tab, newline) from the user input data (with the PHP trim() function).
  • stripslashes($data)will remove backslashes () from the user input data (with the PHP stripslashes() function).
  • htmlspecialchars($data)converts special characters to HTML entities.
    • (If the user inputs <and >, htmlspecialchars() will translate it to &lt;and &gt;).
  • trim($data)将从用户输入数据中去除不必要的字符(额外的空格、制表符、换行符)(使用 PHP trim() 函数)。
  • stripslashes($data)将从用户输入数据中删除反斜杠 ()(使用 PHP stripslashes() 函数)。
  • htmlspecialchars($data)将特殊字符转换为 HTML 实体。
    • (如果用户输入<and >,htmlspecialchars() 会将其转换为&lt;and &gt;)。

回答by kyo

test_input()is not a standard php function but you know you can make your own functions just like this:

test_input()不是标准的 php 函数,但您知道您可以像这样创建自己的函数:

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

Then you can call test_input()in your php script.

然后你可以调用test_input()你的php脚本。

So basically you should ask the author what this does or just search for definition of test_input().

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

If you use frameworks, libraries or plugins, there are tons of non-standard functions defined and what you can is to check the document(api) or to read the source code to find out the answer.

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

Perhaps you can post the whole script, maybe we can find out where test_input()is.

也许你可以把整个脚本贴出来,也许我们可以找出在哪里test_input()

回答by Nitin Pund

There is no any such PHP function like test_input().

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

It might be the user defined function in w3schools website. Just go on the website you mentioned and run the example code and in tryit editor check the source code once, you may get that function there.

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

Some times websites dont display all the functions while giving the explanation, so just refer the source code

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