PHP的具体功能是什么;测试输入();做
我知道我会因此被否决,但我需要问一下。下面的代码是什么意思?明确地 我被这件事弄糊涂了PHP的具体功能是什么;测试输入();做,php,sql,Php,Sql,我知道我会因此被否决,但我需要问一下。下面的代码是什么意思?明确地 我被这件事弄糊涂了 if (empty($_POST["comment"])) { $comment = ""; 这是否意味着如果用户未插入其注释,则值为“$comment”的字段将为空?如果用户插入了注释,那么值“$comment”将通过test_输入函数输入?我看过w3schools的网站,但我一点也不知道。令人难以置信的是,甚至没有一个网站能够描述test_input的功能,相信我,我已经浏览了几十个网站。事实上,我不
if (empty($_POST["comment"])) { $comment = "";
这是否意味着如果用户未插入其注释,则值为“$comment”的字段将为空?如果用户插入了注释,那么值“$comment”将通过test_输入函数输入?我看过w3schools的网站,但我一点也不知道。令人难以置信的是,甚至没有一个网站能够描述test_input的功能,相信我,我已经浏览了几十个网站。事实上,我不是唯一一个认为test_输入是一个神秘函数的用户
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo 'posted';
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
没有像test_input()这样的PHP函数 它可能是w3schools网站中的用户定义函数。只需在您提到的网站上运行示例代码,然后在tryit编辑器中检查一次源代码,您就可以在那里获得该函数 有些时候,网站在给出解释时不会显示所有函数,因此只需参考源代码
test\u input()
不是标准的php函数,但您知道您可以像这样创建自己的函数:
function test_input($input){
$output = $input." Oh, I'm updated."; // Whatever process you want to make
return $output;
}
然后可以在php脚本中调用test\u input()
所以基本上你应该问作者这是做什么的,或者只是搜索test\u input()
的定义
如果您使用框架、库或插件,那么会定义大量非标准函数,您所能做的就是查看文档(api)或阅读源代码以找到答案
也许您可以发布整个脚本,也许我们可以找到
test\u input()
的位置。test\u input
是一个用户定义的函数,而不是内置于PHP中的函数。查看代码确实意味着如果没有插入注释,那么它将是空的,但是如果它不是空的,则通过test_输入函数运行注释
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo 'posted';
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
在这个页面上,我们可以看到他们在哪里声明了test\u 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;
}
?>
因此,如果用户输入了Hello这是一条评论
,$comment
现在将是Hello这是一条评论2014年11月28日16:00:00
因此,最终确定,test\u input
只是w3schools创建的一个函数名,每当页面引用任何类型的输入验证时,他们都会使用它。test\u 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;
}
?>
将从用户输入数据中去除不必要的字符(额外的空格、制表符、换行符)(使用PHP trim()函数)trim($data)
将从用户输入数据中删除反斜杠()(使用PHP stripslashes()函数)stripslashes($data)
将特殊字符转换为HTML实体。htmlspecialchars($data)
- (如果用户输入
,则htmlspecialchars()将其转换为
和
)
- (如果用户输入
注释
字段然后设置为空字符串$comment
。但是,如果它不是空的,则将$comment
设置为测试输入
函数的结果,并将注释作为参数”test\u input
很可能是PHP代码中使用function test\u input
定义的函数。这个问题似乎离题了,因为“test\u input()”不是PHP函数。所提到的函数是定义的。
<?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;
}
?>