1

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';

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Clearer is true. But security is a misattribution, particularly when it's about a parameter called orderBy
Admittedly irrelevant in this case, but I stand by the idea that not declaring where your data is coming from can introduce security vulnerabilities.
Empty fails if the value being checked can be cast to an empty string. this includes '0', ' ', null and the like. Not a big deal if castable-to-empty-string isn't something you need to account for, but it will trip you up sometime later on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.