2016-12-20 28 views
1

[編輯:對於先前的錯誤措辭的道歉。總重寫]重複使用文件中函數的PHP變量

我有一個WordPress站點,我期待在3個相關文件:

  • 的functions.php
  • 的header.php
  • index.php文件(包括header.php文件)

我從外部提要中使用functions.php中的函數提取天氣數據,然後試圖在兩個地方顯示數據 - 一次在header.php中(在網站的每個頁面上)以及一次在index.php(第二個inst僅在主頁上顯示)。我能得到的第一個實例來顯示,但我有麻煩二審

在functions.php中:

function arctic_valley_weather() { 
    function get_string_between($string, $start, $end){ 
     //here's a function that just parses the data. not important 
    } 

    $fullstring = file_get_contents('http://www.cnfaic.org/library/grabbers/nws_feed.php'); 
    $parsed = get_string_between($fullstring, 'arctic_valley,', 'marmot,'); 
    $weatherValues = explode(",",$parsed); 
    $dateTime = date_create($weatherValues[0]); 

    return array(
     'dateTime' => $dateTime, 
     'airTemp' => $weatherValues[1], 
     'relHumid' => $weatherValues[2], 
     'windSpeed' => $weatherValues[3], 
     'windDirection' => $weatherValues[4], 
     'windGust' => $weatherValues[5], 
    ); 
} 

好,然後在header.php中:

$weatherData = arctic_valley_weather(); 
echo round($weatherData['airTemp']); 

這準確地顯示溫度(圓形)。我們說「18」。真棒。

麻煩進來的index.php當我想只是複製該完全相同的結果:

echo round($weatherData['airTemp']); 

這一個不正確地顯示爲「0」,即使最初的實例顯示正確18.

我認爲這就是所有的相關數據。有什麼東西跳出來?

+0

請嘗試創建一個[MVCE](https://stackoverflow.com/help/mcve),或者一段代碼,它可以準確地表明您的問題詠。儘可能減少代碼並將其與網站結構一起發佈(包含哪些文件)。就目前而言,沒有人可以說出了什麼問題。可能是很多事情。 –

+0

我可以說的一件事是,你的函數根本不應該工作,除非你刪除了它的一部分。 '$ weatherValues'沒有在任何地方定義,如果函數在函數外部定義,函數將無法使用它。你將不得不使用'global'關鍵字來使用它。如果它們是全局的,那麼在它進入第二個函數調用之前有可能修改它。 –

+0

謝謝 - 你是對的。這不是很好。我已經將問題重寫得更清楚了。 (功能更多,等等)。不知道我是否仍然留下一些東西,但我不這麼認爲。 – Lime

回答

1

看起來Wordpress在函數體中包含header.php。這意味着一旦您離開header.php,在header.php中定義的所有變量都將超出範圍。您無法訪問index.php,footer.php,page.php等變量。一種解決方案是再次調用您的函數。但是,這會對您的外部資源提出另一個要求,這可能是一種浪費。或者您可以將其分配給超全球$GLOBALS陣列。

看到這個類似的帖子:setting variable in header.php but not seen in footer.php

所以,你會做這樣的事情在你的header.php文件:

$GLOBALS['weatherData'] = arctic_valley_weather(); 
echo $GLOBALS['weatherData']; 

而在你index.php文件中,可以這樣做:

echo $GLOBALS['weatherData']; 
0

首先,你使用全局變量...這不是變量的標準用法(或你應該知道你在做什麼!)

$variable = 'level 0'; 

function test_1() { 
    $variable = 'level 1'; 
    echo $variable; 
} 

echo $variable; // will work, because $variable is set to 'level 0'. 

test_1(); // will work, because $variable is set to 'level 1' ON THE FUNCTION 

function test_2() { 
    echo $variable; 
} 

echo test_2(); // Does not work, because $variable is not set INTERNALLY ON the function 

當然,有可能使用全球變量...但你要做到這一點,重構你的邏輯,你的代碼,以排除這種方法:)

1

嚐嚐這就是這樣,
首先在function.php文件中聲明一個全局變量,然後指定值從你的方法,如thi秒。

global $weatherData; 

function arctic_valley_weather() { 
    global $weatherData; 

    function get_string_between($string, $start, $end) { 
     //here's a function that just parses the data. not important 
    } 

    $fullstring = file_get_contents('http://www.cnfaic.org/library/grabbers/nws_feed.php'); 
    $parsed = get_string_between($fullstring, 'arctic_valley,', 'marmot,'); 
    $weatherValues = explode(",", $parsed); 
    $dateTime = date_create($weatherValues[0]); 

    $weatherData = array(
     'dateTime' => $dateTime, 
     'airTemp' => $weatherValues[1], 
     'relHumid' => $weatherValues[2], 
     'windSpeed' => $weatherValues[3], 
     'windDirection' => $weatherValues[4], 
     'windGust' => $weatherValues[5], 
    ); 
} 

header.phpindex.php嘗試訪問$weatherData這樣的:

global $weatherData; 
echo round($weatherData['airTemp']); 

希望這有助於!