您好我正在寻找一种方法来包含多个PHP变量,而不使用确切的名称,但是一个公共变量(即:$ nav _ ***)有没有人知道这样做的简单方法?
我的目标是能够将这些变量包含在一个数组中,并在我的网页中将它们用作导航栏或类似的东西。
var.php
<?php
$nav_link1 = "link";
$nav_link2 = "link2";
$nav_home = "home";
$nav_search = "search";
?>
proccess.php
$nav = array($nav_***);
nav.php
// haven't created this one yet i'm taking it one step at a time
// new var.php
$nav['home'] = "home.php";
$nav['info'] = "info.php";
$nav['ect'] = "ect";
// would this work the same as an array?
答案 0 :(得分:2)
我正在回答这个问题,因为我无法在评论中展示多行部分。您可以通过设置一个空数组然后在事实之后向它们添加元素来更好地解决您的问题:
$nav = array();
$nav['link1'] = "link";
$nav['link2'] = "link2";
$nav['home'] = "home";
$nav['search'] = "search";
以多行格式一次创建$nav
并不是那么多工作:
$nav = array(
'link1' => 'link',
'link2' => 'link2',
'home' => 'home',
'search' => 'search', // The comma is okay - PHP doesn't mind, try it!
// Add new lines here
);
向此数组声明添加更多行很简单:
$nav = array(
'link1' => 'link',
'link2' => 'link2',
'home' => 'home',
'search' => 'search',
'newlink' => 'newlink',
);
你仍然可以在事后声明更多的值而没有任何问题。如果你这样做,process.php
甚至不需要存在。