我想將一個變量傳遞給一個函數,聲明它的值在該函數內,然後根據第一個函數中聲明的值在第二個函數中使用它。目前下面的代碼只是輸出'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>
還有什麼,因爲你永遠不會用值定義調用你的函數?提示:擺脫全局變量並將所需值作爲參數傳遞(s) – 2014-11-01 10:38:21
而函數結果var_dump'ing無論如何都是NULL,因爲函數'print'出了一些東西,但從不返回編了一個值。 – mario 2014-11-01 10:41:01
我如何用值定義來調用它?這個變量會根據它在第一個函數中設置的內容而有所不同(它最終會是一個多重if循環,它根據不同日期設置變量),所以我不確定該如何去做。 – stmccll 2014-11-01 11:08:02