PHP:当 $_REQUEST 为空时无法设置变量
我在使用 $_REQUEST 或我的语法设置变量时遇到问题。检查 $_REQUEST['orderBy']
时,如果它为空/空,我想设置默认值 'order_date'
。但是,当它检索并清空 $_REQUEST['orderBy']
时,它只是保持为空而不是设置它。我在代码中做了一个丑陋的修复来解决这个问题,但我只是想知道我做错了什么:
$orderBy = isset($_REQUEST['orderBy']) ? stripslashes($_REQUEST['orderBy']) : 'order_date';
I'm having issues with setting a variable either with using $_REQUEST or it's my syntax. When checking $_REQUEST['orderBy']
, if it's blank/empty I want to set a default value of 'order_date'
. However, when it retrieves and empty $_REQUEST['orderBy']
it just remains empty instead of setting it. I made an ugly fix in the code to resolve the issue later on, but I'm just wondering what I'm doing wrong:
$orderBy = isset($_REQUEST['orderBy']) ? stripslashes($_REQUEST['orderBy']) : 'order_date';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这在语法上没有任何问题,但如果
$_REQUEST['orderBy']
设置为空,它会将$orderBy
设置为空值。尝试使用 empty():如果仍然不起作用,您可能是在此行之前错误地设置了
$_REQUEST['orderBy']
。您应该尝试使用更具体的超级全局变量,例如$_POST
和$_GET
,因为它们使您的代码更清晰、更具可读性,而且因为它们提高了您的安全性。应用。There's nothing syntactically wrong with that, but it will set
$orderBy
to an empty value if$_REQUEST['orderBy']
is set but empty. Try using empty():If it still doesn't work, you may be mistakenly setting
$_REQUEST['orderBy']
prior to this line. You should try to use the more specific super globals like$_POST
and$_GET
, both because they make your code clearer and more readable, and because they improve the security of your application.