bremen_matt
bremen_matt

Reputation: 7367

Php: Reliable way to check if there is a query AND whether the GET argument is passed

Really new to PHP...

I would like to have a PHP interface which takes day, month, and year arguments as 'd', 'm', 'y', where the default values are simply pulled from the current date.

My thinking is that that should look something like this:

$day = isset($_GET) && isset($_GET['d']) ? $_GET['d'] : date('d');

But I seem to be missing something. Do I need the first isset($_GET) or is that redundant?

Upvotes: 1

Views: 25

Answers (1)

scrowler
scrowler

Reputation: 24425

$_GET is always going to be set, so yes - that part is redundant.

$day = isset($_GET['d']) ? $_GET['d'] : date('d');

This would be fine for any PHP version, including PHP 5.x.

In PHP 7 you can shorten this by using the null coalescing operator:

$day = $_GET['d'] ?? date('d');

Upvotes: 7

Related Questions