什么是$ _SERVER ['QUERY_STRING']?这个怎么运作?

时间:2013-08-12 10:00:41

标签: php

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

可能$Q的值是多少?

3 个答案:

答案 0 :(得分:12)

例如,您在此浏览器中有一个URL

relation.php?variable1/variable2/variable3

并且您希望获得?之后的值 然后$_SERVER['QUERY_STRING']帮助您确定?

之后的字符串部分

并根据您的问题

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

变量$Q是一个值为

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

查看$_SERVERexplode()

答案 1 :(得分:4)

如果通过任何查询字符串访问页面,$ _SERVER ['QUERY_STRING']将获取该查询字符串。

示例:

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

如果上面的PHP代码以文件名QUERY_STRING.php保存,如果添加'?tutorial = php&amp; section = super-globals'(i.e. QUERY_STRING.php?tutorial=php&section=super-globals);,它会在页面中打印此字符串,因为您已经询问过打印$ SERVER ['QUERY_STRING']的脚本。

更多信息转到:

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

答案 2 :(得分:4)

爆炸:返回一个字符串数组,每个字符串都是字符串的子字符串,通过在字符串分隔符形成的边界上将其拆分而形成。

  

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

运行此代码以理解:

/* A string that doesn't 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 ) );

以上示例将输出:

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

而且,在您的情况下,您的当前查询字符串将被拆分为数组。并且,每个/将是一个数组项。

喜欢如果      爆炸('/','foo / bar')

数组将Foo和Bar包含在单独的索引中。

更多信息: 爆炸:Explode Details from PHP.NET $ _SERVER:$_Server Details from PHP.NET