I am creating pages that are dependent on a query in the url (eg europe.php?country=france). I am aware that it will be useful to re-write theses as europe.php/france with htaccess for SEO etc but what if that page is accessed without the query string?
I am using php to $_GET the query, so if I access the page without the query I get ‘var=;’ ie, it is empty (and retrieves an error). I’m trying to use an if statement to check if the $_GET retrieves nothing but am unsure if this is the right thing to do.
So: how do I check for an un-retrieved var so I can set a default?
Or: am I going about this the wrong way?
If you know the index into
$_GET, useisset():This will only test if the
countryparameter was passed, but it could have been set to an empty string. If this is invalid input, you can combine the check usingempty():You can condense this into one line and save the result to a variable
$countryusing the ternary operator, like so:Finally, you can check if you got absolutely no
$_GETparameters by callingcount()on$_GET: