好吧问题是,当我使用修剪功能不起作用,但当我运行没有修剪功能的代码工作,但没有正常工作(表单接受空格)
<?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,但很难读,我也没弄明白.
正如
PHP manual所说:
empty — Determine whether a variable is empty
如果你真的想要内联if语句,你可以使用类似的东西:
if (!empty($var=trim($_POST['username'])) && !empty($var=trim($_POST['email'])))
但更好的实施应该是:
$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)) { (...)