2

I am trying to check if a URL has a specific query within a URL with multiple parameters:

$parts = parse_url('http://my url?queryA=valueA&queryB=valueB&queryC=valueC ....');
parse_str($parts['query'], $query);

I have used the below to get the value of the query;

$query['queryA']//outputs valueA

However, I want to check if "queryA" is indeed within the URL queries:

echo $parts['query'] // Outputs the full query - queryA=valueA&queryB=valueB&queryC=valueC

// Trying for
if($parts['query'] == 'queryA') {
    // queryA is in the URL
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
BENN1TH
  • 2,003
  • 2
  • 31
  • 42
  • google: "PHP check variable exists" ... [Result #1](http://php.net/manual/en/function.isset.php) - Example #1 part II shows you how to check this for arrays. – Ash Apr 30 '16 at 12:39
  • 1
    @ash That *possible* duplicate you say (or think) it is; that other question is about checking if something `isset()` and not about parsing a url. Hardly a duplicate in my view, just because the accepted answer contains `array_key_exists()` isn't related between both questions. *Two different animals here*. Plus, that other question doesn't even mention `parse_url()`. Best to dig a bit deeper if you're going to mark a question as a duplicate. ;-) – Funk Forty Niner Apr 30 '16 at 13:06
  • @Fred-ii- It is not use the use of `isset` over `array_key_exists`, the OP is trying to determine if a variable exists before using it - therefore, this question shows little effort from the OP to search google and find the answer themselves. The duplicate does show how to test if a variable exists - it may not be 100% the same words but it certainly is 100% the same solution. – Ash Apr 30 '16 at 13:09
  • @ash Well, nobody else thought it was a duplicate (so far anyway), as it wasn't upvoted. If someone does (eventually) upvote it and more than once, sure... why not. Just *"my 2 cents"* on it though. ;-) – Funk Forty Niner Apr 30 '16 at 13:11
  • @Fred-ii- sure, and we could be splitting at hairs. I personally think the reputation rewards for this level of laziness is unacceptable; but then again it's not my call, just my opinion on the matter. – Ash Apr 30 '16 at 13:14
  • Possible duplicate: http://stackoverflow.com/questions/7908163/check-if-variable-exists-and-true – Ash Apr 30 '16 at 13:16
  • @ash - I overflowed the question type with no real solution, so asked it in my own words for a simple solution..inwhich I got from #Saty in a timely manner. – BENN1TH Apr 30 '16 at 13:16
  • Sure, you're every right to. I'm merely objecting to the question (hence my duplicate link) and refusal to up vote (I'm every right to) – Ash Apr 30 '16 at 13:19

1 Answers1

4

Use array_key_exists to check queryA key present in $query array

$parts = parse_url('http://my url?queryA=valueA&queryB=valueB&queryC=valueC');
parse_str($parts['query'], $query);
if (array_key_exists('queryA', $query)) {
    echo "queryA element is in the array";
}
Saty
  • 22,443
  • 7
  • 33
  • 51