2015-01-16 54 views
0

所以我有這個函數(裏面的東西不是很重要,因爲它的工作原理),並且當我打印底部的任何數組時例如$ stdDevArraycomparison),它可以工作。但是,如果我調用函數,然後嘗試打印數組,它不會執行任何操作。爲什麼在調用函數後無法打印數組?爲什麼我不能打印在函數外創建的數組

function TickerResearch ($results, $period, $volinterval) { 
    for ($x = 2; $x < count($resultscomparison) - 1; $x++) { 
     $residualsArraycomparison[$x - 2] = round(($resultscomparison[$x]/$resultscomparison[$x + 1]) - 1, 5); // this is the residuals array that I will use for RSI along with the histograms. 
    } 
    for ($x = 0; $x < count($residualsArraycomparison) - $period; $x++) { 
     for ($y = 0; $y < $period; $y++) { 
      if ($residualsArraycomparison[$x + $y] > 0) { 
       $upcomparison[$x]++; // no need to define it as 0 beforehand. 
      } 
     } 
    } 
    for($x = 2; $x < count($resultscomparison) - $period; $x++) { 
     for ($y = 0; $y < $period; $y++) { 
      $residualscomparison[$y] = ($resultscomparison[$x + $y]/$resultscomparison[$x + $y + 1]) - 1; 
     } 
     $residualsAverage = array_sum($residualscomparison)/count($residualscomparison); 
     for ($y = 0; $y < $period; $y++) { 
      $residualsSub[$y] = pow($residualscomparison[$y] - $residualsAverage, 2); // for std dev 
      $third_moment[$y] = pow($residualscomparison[$y] - $residualsAverage, 3); // for skewness 
      $fourth_moment[$y] = pow($residualscomparison[$y] - $residualsAverage, 4); // for kurtosis 
     } 
     $third_momentSum = array_sum($third_moment); 
     $fourth_momentSum = array_sum($fourth_moment); 
     $variance = array_sum($residualsSub)/count($residualsSub); 
     $stdDevArraycomparison[$x] = pow($variance, 0.5); 
     $skewnessArraycomparison[$x] = $third_momentSum/(($period - 1) * pow($stdDevArraycomparison[$x], 3));  // | These are both similar. Kurtosis is calculated on 
     $kurtosisArraycomparison[$x] = ($fourth_momentSum/(($period - 1) * pow($stdDevArraycomparison[$x], 4)) - 3); // | fours while skewness is calculated on threes. 
    } 
    for ($x = 0; $x < count($upcomparison); $x++) { 
     $upArraycomparison[$x] = 100 - 100/(1 + ($upcomparison[$x]/($period - $upcomparison[$x]))); 
    } 
// print_r($stdDevArraycomparison) would work here. 
} 

TickerResearch($results, $period, $volinterval); 
// print_r($stdDevArraycomparison) WON'T work here. 

回答

2

您需要再次學習函數和變量作用域。如果函數不會返回任何東西,那麼函數外部如何知道它。首先你的功能應該返回你想要的功能以外的所需結果。就像你的函數的最後一條語句應該看起來像

function TickerResearch ($results, $period, $volinterval) { 
    Blah blah blha 
    ....... 
    ....... 
    return $stdDevArraycomparison 
} 

然後趕上結果

$response = TickerResearch($results, $period, $volinterval); 
print_r($response) //will print the result 
+0

這實際上並不是我想要的,因爲我必須輸出四個不同的數組。 –

0

由於範圍變量將不可用,如果你要訪問的變量使全球。

請參閱:link 以及this

0

您需要使$stdDevArraycomparison全球。

$stdDevArraycomparison = array(); 
function TickerResearch ($results, $period, $volinterval) { 
    global $stdDevArraycomparison; 
+0

我試過這樣做,它什麼都沒做。 –

2

您無法打印數組,因爲您所說的打印變量的引用不在函數之外。這個概念被稱爲「可變範圍」。如果您要在函數外部定義相同的變量,那麼它的值將被打印。

這裏是"Variable Scope" in the PHP Manual一個基本的例子:

<?php 
$a = 1; /* global scope */ 

function test() 
{ 
    echo $a; /* reference to local scope variable */ 
} 

test(); 
?> 

一般來說,大多數語言實現的局部和全局變量的作用域的概念。如果變量作用域不存在,則不會有局部變量行爲。在可變範圍內,我們可以在function a()$tempfunction b()中命名變量$temp。這兩個函數將這些變量看作兩個獨立的數據存儲位置,儘管具有相同的人類可讀名稱。

相關問題