2014-11-01 105 views
-1

我想將一個變量傳遞給一個函數,聲明它的值在該函數內,然後根據第一個函數中聲明的值在第二個函數中使用它。目前下面的代碼只是輸出'NULL',沒有別的。任何人都可以幫助我瞭解我要出錯的地方嗎?PHP全局變量內部獨立函數

<?php 

global $mtcurrentLoc; 

function displayCurrentLoc() { 

// Set the current date variable. 
$currentDate = time(); 

// If the current date is greater than the 19th of October and less than the 1st November at 4PM, then print the corresponding address. 
if ($currentDate > strtotime('2014-10-19 00:00:00') && $currentDate < strtotime('2014-11-01 16:00:00')) { 
    print "3 Mills Studio 
        <br/>Three Mill Lane 
        <br/>London 
        <br/>England 
        <br/>E3 3DU"; 
    // Set the current location as a global variable for use in displayMilesTravelled function. 
    global $mtcurrentLoc; 
    $mtcurrentLoc = "London"; 
} 

} 

function displayMilesTravelled() { 
global $mtcurrentLoc; 
switch ($mtcurrentLoc) { 
    case "London": 
     print "244 Miles"; 
     break; 
    case "Plymouth": 
     print "488 Miles"; 
     break; 
    case "Glasgow": 
     print "970 Miles"; 
     break; 
    case "Salford": 
     print "1,185 Miles"; 
     break; 
    case "London2": 
     print "1,400 Miles"; 
     break; 
    case "Surrey": 
     print "1,435 Miles"; 
     break; 
    case "Nottingham": 
     print "1,576 Miles"; 
     break; 
    case "Liverpool": 
     print "1,690 Miles"; 
     break; 
    case "Norwich": 
     print "1,942 Miles"; 
     break; 
    case "Birmingham": 
     print "2,102 Miles"; 
     break; 
    case "Milton Keynes": 
     print "2,173 Miles"; 
     break; 
    case "Bradford": 
     print "2,328 Miles"; 
     break; 
    case "Southampton": 
     print "2,578 Miles"; 
     break; 
    case "Wales": 
     print "2,716 Miles"; 
     break; 
} 
} 

?> 

<p><?php print var_dump(displayMilesTravelled()); ?></p> 
+0

還有什麼,因爲你永遠不會用值定義調用你的函數?提示:擺脫全局變量並將所需值作爲參數傳遞(s) – 2014-11-01 10:38:21

+0

而函數結果var_dump'ing無論如何都是NULL,因爲函數'print'出了一些東西,但從不返回編了一個值。 – mario 2014-11-01 10:41:01

+0

我如何用值定義來調用它?這個變量會根據它在第一個函數中設置的內容而有所不同(它最終會是一個多重if循環,它根據不同日期設置變量),所以我不確定該如何去做。 – stmccll 2014-11-01 11:08:02

回答

0

爲什麼不使用對象?

<?php 

class miles 
{ 

    public $mtcurrentLoc; 
    public $address; 

    function __construct() 
    { 
     // Set the current date variable. 
     $this->currentDate = time(); 
    } 

    function setAddress() 
    { 
     // If the current date is greater than the 19th of October and less than the 1st November at 4PM, then print the corresponding address. 
     if ($this->currentDate > strtotime('2014-10-19 00:00:00') && $this->currentDate < strtotime('2014-11-01 16:00:00')) { 
      $this->address = " 
      3 Mills Studio<br/> 
      Three Mill Lane<br/> 
      London<br/> 
      England<br/> 
      E3 3DU"; 
      // Set the current location as a global variable for use in displayMilesTravelled function. 
      $this->mtcurrentLoc = "London"; 
     } 
    } 

    function displayMilesTravelled() { 

     $this->setAddress(); 

     switch ($this->mtcurrentLoc) { 
      case "London": 
       $this->address.= "<br>244 Miles"; 
       break; 
      case "Plymouth": 
       $this->address.= "<br>488 Miles"; 
       break; 
      case "Glasgow": 
       $this->address.= "<br>970 Miles"; 
       break; 
      case "Salford": 
       $this->address.= "<br>1,185 Miles"; 
       break; 
      case "London2": 
       $this->address.= "<br>1,400 Miles"; 
       break; 
      case "Surrey": 
       $this->address.= "<br>1,435 Miles"; 
       break; 
      case "Nottingham": 
       $this->address.= "<br>1,576 Miles"; 
       break; 
      case "Liverpool": 
       $this->address.= "<br>1,690 Miles"; 
       break; 
      case "Norwich": 
       $this->address.= "<br>1,942 Miles"; 
       break; 
      case "Birmingham": 
       $this->address.= "<br>2,102 Miles"; 
       break; 
      case "Milton Keynes": 
       $this->address.= "<br>2,173 Miles"; 
       break; 
      case "Bradford": 
       $this->address.= "<br>2,328 Miles"; 
       break; 
      case "Southampton": 
       $this->address.= "<br>2,578 Miles"; 
       break; 
      case "Wales": 
       $this->address.= "<br>2,716 Miles"; 
       break; 
     } 

     return $this->address; 
    } 

} 
$miles = new miles(); 

echo '<p>'.$miles->displayMilesTravelled().'</p>'; 

//or if you need a function then do something like 

function displayMilesTravelled(){ 
    global $miles; 
    return $miles->displayMilesTravelled(); 
} 
?> 

<p><?php echo displayMilesTravelled(); ?></p> 
+0

謝謝,至今爲止,這種方法非常有效 - 我想要做的是在網站的一個區域顯示當前位置的地址,然後在另一個區域顯示里程。我將如何將這些相互分離並在兩個不同的區域進行打印?還會有另一個交換機接受當前位置並輸出下一個位置的地址 - 在setAddress函數中實現此功能是否更容易(因此將「address」更改爲currentAddress並添加nextAddress?)。 – stmccll 2014-11-01 13:29:30