Глобальные переменные PHP внутри отдельных функций

Я пытаюсь передать переменную в функцию, объявить ее значение внутри этой функции, а затем использовать ее во второй функции на основе значения, объявленного в первой функции. На данный момент код ниже просто выводит NULL и ничего больше. Кто-нибудь может мне помочь, где я иду не так?

<?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>

-1

Решение

Почему бы не использовать объект?

<?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

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector