php检查URL中是否包含变量

时间:2011-09-12 21:40:58

标签: php url

我正在尝试使用php在我的网址中搜索变量值。这是我的网址示例:
http://example.com/ford/?album=all&gallery=1

我想要构建的脚本来检查我的网址中找到的gallery变量。

5 个答案:

答案 0 :(得分:13)

这可能有所帮助:

isset($_GET['gallery'])

<?php

if(isset($_GET['gallery'])){
   echo "The variable exists!";
}else{
   echo "The variable DOES NOT exist!";
}

?>

isset function

答案 1 :(得分:2)

嗯......图库的价值将在$ _GET ['gallery']中。然后使用if语句if / switch语句,如果你想对它进行检查。

答案 2 :(得分:2)

检查变量是否设置:

if (isset($_GET['gallery'])) {
    $galleryVar = $_GET['gallery'];
    //do whatever
}
else {
    //gallery is not set
}

答案 3 :(得分:2)

正如其他人所说,您可以使用超级全局,例如$_GET$_REQUEST来测试您当前请求的网址的某些请求参数是否存在。

如果你需要检查一些不适合当前请求的任意URL,PHP中有一些内置函数可以提供帮助:

<?php

$url = "http://trucks.com/ford/?album=all&gallery=1";
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $params);
if (isset($params["gallery"])) {
  // yes!
}

另见:

答案 4 :(得分:1)

尝试

<?php
$gallery = $_GET['gallery'];
?>
相关问题