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
}
8
  • google: "PHP check variable exists" ... Result #1 - Example #1 part II shows you how to check this for arrays. Commented Apr 30, 2016 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. ;-) Commented Apr 30, 2016 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. Commented Apr 30, 2016 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. ;-) Commented Apr 30, 2016 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. Commented Apr 30, 2016 at 13:14

1 Answer 1

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";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer, not going to up-vote since this question should be closed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.