2015-01-16 55 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。这两个函数将这些变量看作两个独立的数据存储位置,尽管具有相同的人类可读名称。

相关问题