我试图将一个变量传递给一个函数,在该函数中声明它的值,然后根据第一个函数中声明的值在第二个函数中使用它。目前,下面的代码只是输出“空”'没有别的。任何人都能帮我解决我出错的地方吗?
<?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 :(得分: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>