!empty(trim($ _ POST ['username'] [英] !empty(trim($_POST['username']
问题描述
好吧,问题是当我使用trim函数时不起作用,但是当我运行没有trim函数的代码时,它起作用了,但是却不能正常工作(表单接受空格)
Ok the problem is that when i use the trim function doesnt work but when i run the code without the trim function its working, but not properly working(the form accepts whitespaces)
<?php
session_start();
unset($_SESSION['username']);
if (isset($_SESSION['username']))
{echo "You are in already";}
else if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!empty(trim($_POST['username'])) && !empty(trim($_POST['email'])))
{
$uname = htmlentities($_POST['username']);
$email = htmlentities($_POST['email']);
$_SESSION['username'] = $uname;
echo "THANKS: " . $uname . "<br />";
}
else { echo "fill the goddemn field"; }
} else { ?>
<form action="index.php" method="post">
<label for="username">USERNAME:</label>
<input type="text" name="username" />
<label for="E-MAIL">E-mail:</label>
<input type="text" name="email" />
<input type="submit" value="Enter" />
</form>
<?php } ?>
我尝试了手册 http://php.net/manual/en/function. trim.php ,但是很难阅读,我什么也没弄清楚.
I tried the manual http://php.net/manual/en/function.trim.php but it was hard to read and I didn't figure out anything.
推荐答案
如 PHP手册所述:
空-确定变量是否为空
在您的情况下,trim
是函数调用,而不是变量.
In your case, trim
is a function call, not a variable.
<罢工>
如果您真的想内嵌if
语句,则可以使用类似以下内容的
If you really want to do your if
statement inline, you can use something like:
if (!empty($var=trim($_POST['username'])) && !empty($var=trim($_POST['email'])))
但是更好的实现应该是:
But a better implementation should be:
$username = array_key_exists('username', $_POST) ? trim($_POST['username']) : null;
$email = array_key_exists('email', $_POST) ? trim($_POST['email']) : null;
if (!empty($username) && !empty($email))
{
(...)
这篇关于!empty(trim($ _ POST ['username']的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!