0

i have a php code like this

<?php 
  if(isset($_GET['qry'])) {

   echo $_GET['qry']; 

  }else if(isset($_GET['ct'])) {

   echo $_GET['ct']; 

  } else if(isset($_GET['sct'])) {

   echo $_GET['sct']; 

  } else if(isset($_GET['brand'])) {

   echo $_GET['brand']; 

  } else {

   echo "";

} ?>

what is happening here is that i want to get the value from the variables in the url but somethings they are different on the same page. so please is there a simpler way to do this by getting all the values from the variables and assigning it to a variable inside my code without typing out all the variables in the page something like

eg

$allVariables = $_GET['get all variables'];

if($allVariables == '1'){ 

//do this

}

OR

if($allVariables == '2') {

// do this

}

please is there a way i can do this

brightcode
  • 39
  • 4
  • https://stackoverflow.com/a/5884896/7698734 This answers your question – Hassaan Ali May 03 '20 at 20:56
  • 1
    Yikes! This reminds me of register_globals... this is discouraged for a reason – InterLinked May 03 '20 at 21:09
  • @brightcode what's the status of this question? Did my answer solve it? – compuphys May 06 '20 at 11:57
  • @compuphys am really orry for not giving the feedback about this but it didn't work. cause what i wanted to know is that if its possible to get multiple variables in the same url at the same time – brightcode May 06 '20 at 20:45
  • @brightcode it is possible, my answer tells you that. `$_GET` returns all parameters passed through the URL. If your URL is `www.somesite.com/index.php?var1=value1&var2=value2`, then `$_GET` is an array containing `var1` with a value of `value1` and `var2` with a value of `value2`. – compuphys May 06 '20 at 20:49
  • Also, if you don't think an answer is correct or doesn't work for you then please don't just ignore it. At least comment to say why you believe it's wrong. That way we can work together to come to a working solution. People put time and effort into answering questions, so ignoring their efforts is pretty rude. – compuphys May 06 '20 at 20:53
  • @compuphys how do you do it using the get to get all parameters cause I only know how to get once – brightcode May 06 '20 at 21:47
  • @brightcode I’m not sure how I can be any more clear than in my answer and comment. `$_GET` *does* contain all of the variables passed in a url query string! – compuphys May 07 '20 at 07:47
  • @compuphys alright thank you very much I really appreciate it – brightcode May 07 '20 at 09:07
  • @brightcode no problem. Please be aware of this going forward: https://stackoverflow.com/help/someone-answers – compuphys May 07 '20 at 18:00

1 Answers1

1

$_GET is an example of a superglobal in php:

Superglobals are built-in variables that are always available in all scopes.

and $_GET in particular is:

An associative array of variables passed to the current script via the URL parameters (aka. query string).

You can check if a $_GET parameter is set and set a variable equal to its value in one step using the null coalescing operator:

$var = $_GET['some_parameter'] ?? null

In this case $var equals the value of $_GET['some_parameter'] if it exists, or null otherwise.

~~~~~ EDIT ~~~~~

As pointed out in the comments, this operator is only available in php 7.2.0 onward. To achieve the same in versions < 7.2, use the ternary operator:

$var= isset($_GET['some_parameter']) ? $_GET['some_parameter'] : null;

~~~~~~~~~~~~~~~


In your code where you check if($allVariables == '1'){//do this}, if you're actually trying to check the number of parameters passed through the URL you should use the count() function.

if (count($_GET) == 1) {
  // do this
} else if (count($_GET) == 2) {
  // do this instead
}

... but this would be of limited use as it doesn't tell you anything about which parameters have been passed.

compuphys
  • 1,289
  • 12
  • 28
  • 1
    If should be noted (in the answer) that the `??` operator is exclusive to PHP 7+. – Funk Forty Niner May 03 '20 at 21:14
  • I know it's not a requirement but it's definitely frustrating when you go through the effort of pulling in php manual links and giving detailed explanations to try to help people and get no response at all. – compuphys May 03 '20 at 21:29