PHP check if url parameter exists
To check if a URL parameter exists in PHP, you can use the isset
function. This function returns a boolean value indicating whether a variable with the specified name has been set and is not NULL
.
For example, if you want to check if a URL parameter called id
exists, you can use the following code:
<?php
if (isset($_GET['id'])) {
// URL parameter exists
echo 'URL parameter exists';
} else {
// URL parameter does not exist
echo 'URL parameter does not exist';
}
Watch a video course
Learn object oriented PHP
You can also use the array_key_exists
function to check if a key exists in an array. This function can be used to check if a URL parameter exists in the $_GET
array:
<?php
if (array_key_exists('id', $_GET)) {
// URL parameter exists
echo 'URL parameter exists';
} else {
// URL parameter does not exist
echo 'URL parameter does not exist';
}