0

So I have this function (what's inside isn't really important because it works), and when I print any of the arrays at the bottom (eg. $stdDevArraycomparison), it works. However if I call the function and then try to print the array, it doesn't do anything. Why can't I print the array after calling the function?

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.

4 Answers4

2

You cannot print the array because your reference to print the variable is outside of the function, as you said. That concept is called "variable scope". If you were to define the same variable outside of the function, then it's value would be printed.

Here is a basic example from "Variable Scope" in the PHP Manual:

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

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

test();
?>

Generally speaking, most languages implement the concept of local and global variable scope. If variable scope did not exist, there would be no local variable behavior. With variable scope, we can name a variable $temp in function a() and $temp in function b(). The two functions see those variables as two separate data storage locations, despite having the same human-readable name.

sealocal
  • 10,897
  • 3
  • 37
  • 50
1

You need to learn functions and variable scopes again. If function will not return any thing then how it is known to outside the function. First your function should return the desired result that you want outside the function. Like the last statement of your function should look like

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

Then catch the result

$response = TickerResearch($results, $period, $volinterval);
print_r($response) //will print the result
Awais Qarni
  • 17,492
  • 24
  • 75
  • 137
0

Due to Scope that variable will not be available,if you want to access that variable make it global.

Refer this : link and also this .

Community
  • 1
  • 1
Afsar
  • 3,104
  • 2
  • 25
  • 35
0

You need to make $stdDevArraycomparison global.

$stdDevArraycomparison = array();
function TickerResearch ($results, $period, $volinterval) {
  global $stdDevArraycomparison;
Misunderstood
  • 5,534
  • 1
  • 18
  • 25