2012-11-08 214 views
0

如果我需要使用$ foo的功能,全局變量和函數

而且global $foo是如此令人難以接受的,

我怎樣才能讓我的功能,沒有它是全球訪問的變量?有沒有另外一種方法或方法來做到這一點,我不知道?

+0

爲什麼不把它作爲一個參數? – air4x

+0

命令式編碼器肯定會不同意,但是如果'global $ foo'被皺眉取決於實際的變量名稱和用法。如果你使用的是這樣一個淺的變量名稱,可能還有其他十幾個變量名稱,那麼你正在污染共享範圍,並且可能會打開無意覆蓋或濫用信號的大門。 – mario

回答

0

嘗試是這樣的..

<?php 
    $name = "batman"; 

    function get_details($name,$age){ 
     $temp_str = "Name: ".$name."<br />Age: ".$age; 
     return($temp_str); 
    } 

    echo get_details($name,35); // passing through name as a variable and the age as just text... 
?> 

給...

Name: batman 
Age: 35