I would like to get the parameter in a key such as http://link.com/?parameter
I read somewhere that the key is NULL
and the value would be parameter
.
I tried $_GET[NULL]
but it didn't seem to work.
Here is my code:
if ($_GET[NULL] == "parameter") {
echo 'Invoked parameter.';
} else {
echo '..';
}
It just prints out ..
so that means I'm not doing it right. Can someone please show me the right way.
There are no such things as keyless URL parameters in PHP. ?parameter
is the equivalent of $_GET['parameter']
being set to an empty string.
Try doing var_dump($_GET)
with a url like http://link.com/?parameter and see what you get.
It should look something like this:
array (size=1) 'parameter' => string '' (length=0)
Thus, you can test it in a couple of ways depending on your application needs:
if (isset($_GET['parameter'])) {
// do something
} else {
// do something else
}
or
// Only recommended if you're 100% sure that 'parameter' will always be in the URL.
// Otherwise this will throw an undefined index error. isset() is a more reliable test.
if ($_GET['parameter'] === '') {
// do something
} else {
// do something else
}
$_GET
is a super global assoc
array, that contains parameter
=> it's value
pairs. So, parameter
is a key of the array.
For example, if your url is something like this: myweb.com/?page=load&do=magic
than you $_GET
is:
$_GET(
[page] => load
[do] => magic
)
If you want just to test, if parameter
is in you URL as a param, you should do something like this:
if (isset($_GET['parameter'])
echo "Here I am!";
You can also get the entire request_url like this
echo $_SERVER['REQUEST_URI'];
To get the part after ?
:
echo explode('?', $_SERVER['REQUEST_URI'])[1];
You could via array_keys()
:
$param = array_keys($_GET)[0];
Which would give you the name of the first querystring parameter, whether it has a value or not.
You could also get all of the parameters without a value like so:
$empty = [];
foreach($_GET as $key => $value)
if(strlen($value) === 0) $empty[] = $key;
print_r($empty);
You can get the part of the URL beginning with ?
with
$_SERVER['QUERY_STRING']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.