0

I have a url of the format /?var1 = val1&var2 = val2;

I want to check for the presence of just var2 = val2 in the url using php. How can that be done?

3 Answers 3

1

You can use the $_GET superglobal

if(!empty($_GET['var2']) && $_GET['var2'] == 'val2')) // do something

Sign up to request clarification or add additional context in comments.

Comments

1

do this

if (isset($_GET['var2']))  // check if exist
{ 
  if ($_GET['var2'] == val2) // do something
}

1 Comment

Or, combined if (isset($_GET['var2']) && $_GET['var2'] == val2). Due to short-circuiting.
1

If this is not the QUERY_STRING for the request (in which case you should just use $_GET as @Spechal said), you can use parse_str(). It "uses the same mechanism that PHP uses to populate the $_GET, $_POST, etc. variables."

See also parse_url().

Comments

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.