通过查询字符串 (php) 传递变量 [英] Passing Variables via Query String (php)
问题描述
我正在通过在线课程慢慢学习 PHP.一个特定的练习是这样的:
I'm slowly learning PHP through an online course. A particular exercise goes like so:
html 页面:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Greeting the Beatles</title>
</head>
<body>
Choose a Beatle to greet.
<ul>
<li><a href="HelloWho.php?Beatle=Paul">Paul</a></li>
<li><a href="HelloWho.php?Beatle=John">John</a></li>
<li><a href="HelloWho.php?Beatle=George">George</a></li>
<li><a href="HelloWho.php?Beatle=Ringo">Ringo</a></li>
</ul>
</body>
</html>
对应的php页面:
<?php
$beatle = $_GET['Beatle'];
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Hello <?php echo $beatle ?>!</title>
</head>
<body>
<?php
echo "Hello $beatle!";
?>
</body>
</html>
我的代码与上面的相同,但我一直收到一个页面说我在第 2 行有一个未定义的索引:
My code is identical to the above and yet I keep getting a page saying I have an undefined index on line 2:
Notice: Undefined index: Greet in /Applications/XAMPP/xamppfiles/htdocs/Webucator/ClassFiles/Webucator/ClassFiles/PHPBasics/Exercises/HelloWho.php on line 2
World!
我意识到这是一个愚蠢的问题,但我还没有看到答案,除了我还没有了解的 isset() 函数.
I realize this is a dumb question, but I haven't seen an answer for it yet, aside from the isset() function which, I haven't learned about yet.
推荐答案
您发布的代码运行正常,并且没有给出您发布的错误.
The code you posted does work properly, and does not give the error that you posted.
阅读错误内容可以轻松解决您的问题.它说第 2 行有一个未定义的索引Greet".
Reading what the error says can easily fix your problem. It says there is an undefined index "Greet" on line 2.
确保您的副本中的第 2 行与工作版本相同
Ensure that in your copy you have the same line 2 as the working version
$beatle = $_GET['Beatle'];
如果你仔细观察它是如何工作的,你会看到你传递数据的 url 是
If you look closer at how this works, you see the url that you are passing data to is
HelloWho.php?Beatle=NameGoesHere
PhP文件能够从
?Beatle=
通过使用
$_GET['Beatle']
并将它存储在一个变量中,就像它在第二行一样
and storing it in a variable like it has in line two
$beatle = $_GET['Beatle'];
此外,我知道你说你还没有学习它,但是你可以使用isset()来检查字符串是否存在
Moreover, I know you said you have yet to learn it, but you can use isset() to check if the string exists
if (isset($_GET['Beatle'])) {
$beatle = $_GET['Beatle'];
echo "Hello $beatle!";
}
这篇关于通过查询字符串 (php) 传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!