Reputation: 152
I need to get many parameters from a url with different names and check if they are set. (their value doesn't necessarily matter).
https://example.com?one=true&two=true&three=true etc.
The problem is that in what I want to do, any of them could be set or not.
So I need help of a way to grab which of them are in the url and are set in a clean way, and preferably stored in different variables matching the name of the parameters, instead of having multiple $_GET
and isset()
lines with if
statements all over the place.
EDIT: SUBMIT BEING A PARAMETER WHICH WILL ALWAYS BE IN THE URL: I was thinking this could just be done using a foreach loop.
if (isset($_GET['submit'])) {
foreach ($_GET as $key => $value) {
$key = $value;
echo $key;
}
}
this will echo out the value of the $key, (which is all i need because then i know it is set), but then i need to get the actual name of what $key is set to.
Thanks
EDIT 2: I have found out how to do this - all i needed was to get the parameters in the url and know the name of them. Sorry if i had worded it wierdly.
if (isset($_GET['submit'])) {
foreach ($_GET as $key => $value) {
$$value = $key;
echo $key;
}
}
I found out using variable variables is what i needed (hence the 2 $$) This gives me, in the end, $key as the name of whatever parameters are in the url, stored in $value which has the same name.
Upvotes: 1
Views: 104
Reputation: 8249
You need to use extract()
to extract all keys from an array and treat them as variables
Import variables into the current symbol table from an array
Here is a small example:
$_GET['var_1'] = 1;
$_GET['var_2'] = 2;
$_GET['var_3'] = '';
extract($_GET);
echo $var_1;
Use below code to get empty as well as set keys/values:
$newArray = array_filter($_GET);
$sub_key = array_keys($newArray);
print_r($sub_key); // this will give you all keys that have values
$sub_values = array_values($newArray);
print_r(array_diff($_GET, $sub_values)); // all keys that have empty values
Upvotes: 1
Reputation: 12505
If there is possibility that a key is there or not there, it's helpful to have a class/function that can do a check and return. A framework might work something like this:
class Request
{
public static function getGet($key=false)
{
if(!empty($key))
return (isset($_GET[$key]))? $_GET[$key] : false;
return $_GET;
}
public static function getPost($key=false)
{
if(!empty($key))
return (isset($_POST[$key]))? $_POST[$key] : false;
return $_POST;
}
}
# Would try to retrieve $_GET['one']
$val1 = Request::getGet('one');
# Would try to retrieve $_POST['two']
$val2 = Request::getPost('two');
I wouldn't create dynamic variables, because you still have to check if they are set or whatever. Depending on use and PHP version, you can do:
if(Request::getGet('whatever')) {
# Do something
}
Upvotes: 0
Reputation: 6348
You can check if all of the parameters are provided by checking the keys of $_GET
$expected_parameters = ['one', 'two', 'three'];
if(count(array_intersect($expected_parameters, array_keys($_GET))) !== count($expected_parameters)) {
// Failed validation
}
That will validate that all the required parameters are present, but it doesn't make sure that they're not empty. I would recommend doing further validation to make sure the parameters are of the required type, but in your question you said this isn't a requirement.
To get the parameters into their own variables you can use extract()
.
extract($_GET)
Upvotes: 0