如何使用URL传递PHP变量 [英] How to pass a PHP variable using the URL

查看:64
本文介绍了如何使用URL传递PHP变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用URL传递一些PHP变量.

I want to pass some PHP variables using the URL.

我尝试了以下代码:

link.php

<html>
<body>
<?php
$a='Link1';
$b='Link2';
echo '<a href="pass.php?link=$a">Link 1</a>';
echo '<br/>';
echo '<a href="pass.php?link=$b">Link 2</a>';
?></body></html>`</pre></code>

pass.php
<pre><code>`<html>
<body>
<?php
if ($_GET['link']==$a)
{
echo "Link 1 Clicked";
} else {
echo "Link 2 Clicked";
}
?></body></html>

单击链接Link1Link2时,出现链接2已单击".为什么?

Upon clicking the links Link1 and Link2, I get "Link 2 clicked". Why?

推荐答案

在您的link.php中,您的echo语句必须像这样:

In your link.php your echo statement must be like this:

echo '<a href="pass.php?link=' . $a . '>Link 1</a>';
echo '<a href="pass.php?link=' . $b . '">Link 2</a>';

然后在pass.php中,您不能使用$a,因为它没有使用预期的字符串值初始化.

Then in your pass.php you cannot use $a because it was not initialized with your intended string value.

您可以将其直接与这样的字符串进行比较:

You can directly compare it to a string like this:

if($_GET['link'] == 'Link1')

另一种方法是首先将变量初始化为与使用link.php相同的操作.而且,更好的方法是将$a$b变量包含在单个PHP文件中,然后将其包含在所有页面中,您将在这些页面中使用这些变量,如Tim Cooper在他的帖子中提到的那样.您也可以将其包含在会话中.

Another way is to initialize the variable first to the same thing you did with link.php. And, a much better way is to include the $a and $b variables in a single PHP file, then include that in all pages where you are going to use those variables as Tim Cooper mention on his post. You can also include this in a session.

这篇关于如何使用URL传递PHP变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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