Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 函数的全局变量更改';s返回值_Php_Function_Global Variables_Return - Fatal编程技术网

Php 函数的全局变量更改';s返回值

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

在我的网站上,我有一个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","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'>&raquo; ".$p."</span></span>";}
        else {$menu=$menu."<a href='".$link."'><span class='button'><span class='textspace'>&raquo; ".$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'>&raquo; ".$o."</span></span>";}
        else {$menu=$menu."<a href='".$link."'><span class='button'><span class='textspace'>&raquo; ".$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);
?>