通过查询字符串 (php) 传递变量

编程入门 行业动态 更新时间:2024-10-28 14:22:30
本文介绍了通过查询字符串 (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) 传递变量

本文发布于:2023-07-07 09:05:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1061390.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   变量   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!