将值赋给数组然后引用它们

时间:2021-10-11 21:41:46

Not sure why I am having such a problem with this

不知道为什么我遇到这样的问题

$page = $_GET['page'];
$pagename = array(home => My Home, about => About Us, contact => Contact Us);

echo "You are on the" . $pagename[$page] . "page";

the URL would be

URL将是

http://www.mywebsite.com/subpage.php?page=contact

The echo should say "You are on the Contact Us page.

回声应该说“你在联系我们页面。

3 个解决方案

#1


2  

your array definition is using constants instead fo strings as keys. add quotes to keys.

你的数组定义使用常量而不是字符串作为键。在键上添加引号。

array('home' => 'My Home', 'about' => 'About Us', 'contact' => 'Contact Us'); 

#2


1  

Maybe it's because you lack "" in your string?

也许是因为你的字符串中缺少“”?

$pagename = array("home" => "My Home", "about" => "About Us", "contact" => "Contact Us");

#3


1  

You didn't mention your problem. Looking only at your code, it looks like you forgot to quote your strings in the array.

你没有提到你的问题。仅查看代码,看起来您忘记在数组中引用字符串。

$pagename = array(home => My Home, about => About Us, contact => Contact Us);

Would become

$pagename = array('home' => 'My Home', 'about' => 'About Us', 'contact' => 'Contact Us');

#1


2  

your array definition is using constants instead fo strings as keys. add quotes to keys.

你的数组定义使用常量而不是字符串作为键。在键上添加引号。

array('home' => 'My Home', 'about' => 'About Us', 'contact' => 'Contact Us'); 

#2


1  

Maybe it's because you lack "" in your string?

也许是因为你的字符串中缺少“”?

$pagename = array("home" => "My Home", "about" => "About Us", "contact" => "Contact Us");

#3


1  

You didn't mention your problem. Looking only at your code, it looks like you forgot to quote your strings in the array.

你没有提到你的问题。仅查看代码,看起来您忘记在数组中引用字符串。

$pagename = array(home => My Home, about => About Us, contact => Contact Us);

Would become

$pagename = array('home' => 'My Home', 'about' => 'About Us', 'contact' => 'Contact Us');