开发者

PHP: Can't set variable when $_REQUEST is blank

开发者 https://www.devze.com 2023-02-22 13:05 出处:网络
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\'. H

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 r开发者_Python百科emains 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';


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():

$orderBy = (empty($_REQUEST['orderBy'])) ? 'order_date' : $_REQUEST['orderBy'];

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消