Php 函数的全局变量更改';s返回值
在我的网站上,我有一个PHP文件,它从一个外部PHP文件生成菜单(以便于更新) 下面是PHP的一部分,概述了问题:Php 函数的全局变量更改';s返回值,php,function,global-variables,return,Php,Function,Global Variables,Return,在我的网站上,我有一个PHP文件,它从一个外部PHP文件生成菜单(以便于更新) 下面是PHP的一部分,概述了问题: $products = array("Kinetic","Key","Twister","Ellipse","Focus","Trix","Classic","Pod","Halo","Alu","Rotator","Canvas","Image","Flip","Executive","Torpedo","Nature","Wafer Card","Alloy Card","Bo
$products = array("Kinetic","Key","Twister","Ellipse","Focus","Trix","Classic","Pod","Halo","Alu","Rotator","Canvas","Image","Flip","Executive","Torpedo","Nature","Wafer Card","Alloy Card","Bottle Opener","Ink Pen","Clip","Light","Tie Clip","Event Lanyard","Lizzard Wristband","Slap Wristband");
$others = array("Other 1","Other 2","Other 3","Other 4","Other 5");
function genMenu($product) {
global $products;
global $others;
$menu="<div class='titlebar'><div class='title'>USBs</div></div><div class='buttons'>";
for ($x=0;$x<count($products); $x++) {
$p=$products[$x];
$link=strtolower(str_replace(' ', '', $p)).".php";
if($product==$p) {$menu=$menu."<span class='activebutton'><span class='textspace'>» ".$p."</span></span>";}
else {$menu=$menu."<a href='".$link."'><span class='button'><span class='textspace'>» ".$p."</span></span></a>";}
if(count($products)>$x+1) {
$menu=$menu."<img src='layout/seperator.jpg' class='seperator' alt='' width='184' height='2' />";
}
}
$menu=$menu."</div><div class='titlebar'><div class='title'>Other Products</div></div><div class='buttons'>";
for ($y=0;$y<count($others); $y++) {
$o=$others[$y];
$link=strtolower(str_replace(' ', '', $o)).".php";
if($product==$o) {$menu=$menu."<span class='activebutton'><span class='textspace'>» ".$o."</span></span>";}
else {$menu=$menu."<a href='".$link."'><span class='button'><span class='textspace'>» ".$o."</span></span></a>";}
if(count($others)>$y+1) {
$menu=$menu."<img src='layout/seperator.jpg' class='seperator' alt='' width='184' height='2' />";
}
}
$menu=$menu."</div>";
return (string)$menu;
}
function genDrop($product) {
global $products;
global $others;
$drop=$products[12];
return $drop;
}
我试过你的代码,
genDrop()
返回“Image”
。有些东西你没给我们看。genDrop()
从不调用genMenu()
,是不是?问题最有可能出现在genMenu()
的调用者中,您尚未显示该调用者。请尽量避免global
。为什么不把它转换成一个类呢?@Barmar运行此代码之前唯一没有显示的是HTML,它调用了这两个函数,并将其添加到原始帖子中。$products=genMenu($product)
重新分配$products
。那么,为什么您对$products
的变化感到惊讶呢?
<?php include("menufoot.php");
$product="Twister";
$products = genMenu($product);
$dropmenu = genDrop($product);
print $dropmenu;
$footer = genFoot($product);
$mobilefooter = genMobFoot($product);
?>