函数返回值的全局变量变化

时间:2013-06-20 01:56:24

标签: 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","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;
}

因此,菜单使用我在函数外部的产品数组生成,通过使它们全局化来实现它们的使用...我遇到的问题是当函数返回$menu时,它也会改变{{ 1}}值相同。 (但不是$products

因此第二个函数($others)从genDrop

返回't'

这是否意味着发生?我怎么阻止它?

调用函数的HTML中的PHP:

$menu="<div class='t

0 个答案:

没有答案