本文介绍了!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 } ?>我尝试了手册 php/manual/en/function. trim.php ,但是很难阅读,我什么也没弄清楚.
I tried the manual php/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($
发布评论