php – what is $_SERVER[QUERY_STRING] ? how it works?

php – what is $_SERVER[QUERY_STRING] ? how it works?

for example you have a URL in browser like this

relation.php?variable1/variable2/variable3

and you want the get the value after the ?
then $_SERVER[QUERY_STRING] helps you to determine the part the string after the ?

and according to your question

$Q = explode(/, $_SERVER[QUERY_STRING]);

variable $Q is an array with the values like

Array
(
  [0] => variable1
  [1] => variable2
  [2] => variable3
)

take a look on the $_SERVER and explode()

Explode : Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.

array explode ( string $delimiter , string $string [, int $limit ] )

Run this Code to Understand :

/* A string that doesnt contain the delimiter will simply return a one-length array of the original string. */

$input1 = hello;
$input2 = hello,there;
var_dump( explode( ,, $input1 ) );
var_dump( explode( ,, $input2 ) );

The above example will output:

array(1)
(
    [0] => string(5) hello
)
array(2)
(
    [0] => string(5) hello
    [1] => string(5) there
)

And, In Your Case, Your Current Query String will be Splited into Array. And, Each / will be a array item.

Like if
explode( /, foo/bar)

Array will contain Foo and Bar into seperate index.

For More :
Explode : Explode Details from PHP.NET
$_SERVER : $_Server Details from PHP.NET

php – what is $_SERVER[QUERY_STRING] ? how it works?

If a page is accessed via any query string, $_SERVER[QUERY_STRING] fetches that query string.

Example :

<?php  
echo The query string is: .$_SERVER[QUERY_STRING];  
?>  

If the above php code is saved with a filename of QUERY_STRING.php and if you add ?tutorial=php&section=super-globals (i.e. QUERY_STRING.php?tutorial=php&section=super-globals); it will print this string in the page since you have asked the script to print $SERVER[QUERY_STRING].

For more info goto :

http://php.net/manual/en/reserved.variables.server.php

Leave a Reply

Your email address will not be published.