PHP检查url参数是否存在

时间:2021-11-05 08:28:48

I have a URL which i pass parameters into

我有一个URL,我将参数传递给

example/success.php?id=link1

例如/ success.php?ID = LINK1

I use php to grab it

我用php来抓住它

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

then an if statement to display content based on parameter

然后是一个if语句来显示基于参数的内容

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

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.

只需要知道在PHP中如何说,如果url param存在抓住它并执行if函数,如果它不存在则什么都不做。

Thanks Guys

多谢你们

5 个解决方案

#1


74  

Use isset()

使用isset()

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

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.

编辑:这是为了完整性添加。 php中的$ _GET是一个保留变量,它是一个关联数组。因此,您还可以使用'array_key_exists(mixed $ key,array $ array)'。它将返回一个找到或不找到密钥的布尔值。所以,以下也可以。

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

#2


37  

if(isset($_GET['id']))
{
    // Do something
}

You want something like that

你想要那样的东西

#3


3  

It is not quite clear what function you are talking about and if you need 2 separate branches or one. Assuming one:

目前还不是很清楚你在谈论什么功能,以及你是否需要2个独立的分支机构。假设一个:

Change your first line to

将第一行更改为

$slide = '';
if (isset($_GET["id"]))
{
    $slide = $_GET["id"];
}

#4


1  

Here is the PHP code to check if 'id' parameter exists in the URL or not:

以下是用于检查URL中是否存在“id”参数的PHP代码:

if(isset($_GET['id']))
{
   $slide = $_GET['id'] // Getting parameter value inside PHP variable
}

I hope it will help you.

我希望它会对你有所帮助。

#5


0  

Why not just simplify it to if($_GET['id']). It will return true or false depending on status of the parameter's existence.

为什么不将它简化为if($ _ GET ['id'])。它将根据参数的存在状态返回true或false。

#1


74  

Use isset()

使用isset()

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

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.

编辑:这是为了完整性添加。 php中的$ _GET是一个保留变量,它是一个关联数组。因此,您还可以使用'array_key_exists(mixed $ key,array $ array)'。它将返回一个找到或不找到密钥的布尔值。所以,以下也可以。

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

#2


37  

if(isset($_GET['id']))
{
    // Do something
}

You want something like that

你想要那样的东西

#3


3  

It is not quite clear what function you are talking about and if you need 2 separate branches or one. Assuming one:

目前还不是很清楚你在谈论什么功能,以及你是否需要2个独立的分支机构。假设一个:

Change your first line to

将第一行更改为

$slide = '';
if (isset($_GET["id"]))
{
    $slide = $_GET["id"];
}

#4


1  

Here is the PHP code to check if 'id' parameter exists in the URL or not:

以下是用于检查URL中是否存在“id”参数的PHP代码:

if(isset($_GET['id']))
{
   $slide = $_GET['id'] // Getting parameter value inside PHP variable
}

I hope it will help you.

我希望它会对你有所帮助。

#5


0  

Why not just simplify it to if($_GET['id']). It will return true or false depending on status of the parameter's existence.

为什么不将它简化为if($ _ GET ['id'])。它将根据参数的存在状态返回true或false。