2013-02-16 192 views
-1

我有PHP编写一些简单的代码,我不知道为什么它不会工作:(简单的PHP代码不起作用

的第一个PHP文件:

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Title</title> 
</head> 
<body> 
<?php include_once "php/codes.php"; ?> 

<ul> 
<?php echo "<li><a href='' class='".whereAmI("files")."'>YYYYY</a></li> ";?> 
<li><a href=''>XXXX</a></li> 
    </ul> 
</body> 
</html> 

而且第二个(codes.php):

<?php 
$menu= array("privileges","files", "posts", "menu"); 

    function whereAmI($addr){ 
     foreach ($menu as $value) { 
      if($value===$addr) return "current"; 

    } 
} 

的问题是,该网页无法显示任何东西,我不知道为什么

+0

启用错误代码,你会看到为什么 – 2013-02-16 21:26:36

+0

尝试在顶部坚持这你的代码:ini_set('error_reporting',E_ALL); – Stuart 2013-02-16 21:27:53

+0

如果它没有执行,你要么有错误,要么你的环境没有被设置为正确执行php。 – datasage 2013-02-16 21:27:54

回答

3

虽然我一个?对全局变量男,你的代码改成这样:

<?php 
$menu= array("privileges","files", "posts", "menu"); 

    function whereAmI($addr){ 
     global $menu; 
     foreach ($menu as $value) { 
      if($value===$addr) return "current"; 

    } 
} 
?> 
+0

Thans它工作正常:) 你能告诉我为什么有必要使用“全局”吗? – PatLas 2013-02-16 21:34:38

+0

http://php.net/manual/en/language.variables.scope.php – BBagi 2013-02-16 21:39:43

+0

@PatLas阅读:[如何接受答案的作品?](http://meta.stackexchange.com/questions/5234/how-does -accepting-的回答工作?answertab =#投票制表顶部) – 2013-07-23 21:59:15