PHP检查url参数是否存在 [英] PHP check if url parameter exists

查看:237
本文介绍了PHP检查url参数是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个URL,我可以将参数传递到其中

I have a URL which i pass parameters into

example/success.php?id = link1

example/success.php?id=link1

我用php来抓

$slide = ($_GET["id"]);

然后使用if语句显示基于参数的内容

then an if statement to display content based on parameter

<?php  if($slide == 'link1') { ?>
   //content
 } ?>

只需在PHP中知道怎么说,如果url参数存在,就抓住它并执行if函数,如果它不存在,则什么也不做.

Just need to know in PHP how to say, if the url param exists grab it and do the if function, if it doesn't exist do nothing.

谢谢你们

推荐答案

使用 isset( )

$matchFound = (isset($_GET["id"]) && trim($_GET["id"]) == 'link1');
$slide = $matchFound ? trim($_GET["id"]) : '';

编辑: 出于完整性考虑,添加了此内容. $ _GET 保留的变量,这是关联数组.因此,您还可以使用'array_key_exists(混合$ key ,数组$ array)'.它将返回一个布尔值,表明是否找到了密钥.因此,以下内容也可以.

EDIT: This is added for the completeness sake. $_GET in php is a reserved variable that is an associative array. Hence, you could also make use of 'array_key_exists(mixed $key, array $array)'. It will return a boolean that the key is found or not. So, the following also will be okay.

$matchFound = (array_key_exists("id", $_GET)) && trim($_GET["id"]) == 'link1');
$slide = $matchFound ? trim($_GET["id"]) : '';

这篇关于PHP检查url参数是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆