!empty(trim($

编程入门 行业动态 更新时间:2024-10-26 14:31:37
本文介绍了!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($

本文发布于:2023-10-12 16:28:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485270.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:empty   trim

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!