Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a global array

Tags:

php

global

Hi. I recently learned PHP and am trying to declare a global array so I can access inside a function. But I seem to be missing something because I get the error 'Undefined variable:'

Here is my code:

global $second_array;
$second_array = array();

function operatii($v) {
  $var1 = $second_array[count($second_array)-1];
  $var2 = $second_array[count($second_array)-2];
  $rez = null;

  echo $var1 . $var2 . "este?";
}

for ($i = 0; $i < count($a); $i++){
  if ($a[$i] === "+" || $a[$i] === "-" || $a[$i] === "*" || $a[$i] === "/" ) {
    operatii($a[$i]);
  } else {
    array_push($second_array, $a[$i]);
  }
}

I seem to be able to use the $second_array in the for loop, but can't use it in the operatii function.
How can I solve this problem?

like image 879
Nistor Alexandru Avatar asked Oct 13 '12 19:10

Nistor Alexandru


People also ask

Can you have a global array?

As in case of scalar variables, we can also use external or global arrays in a program, i. e., the arrays which are defined outside any function. These arrays have global scope. Thus, they can be used anywhere in the program. They are created in the beginning of program execution and last till its end.

How do you declare an array?

To create an array, define the data type (like int ) and specify the name of the array followed by square brackets []. To insert values to it, use a comma-separated list, inside curly braces: int myNumbers[] = {25, 50, 75, 100}; We have now created a variable that holds an array of four integers.


2 Answers

There are two ways to reference a global variable in PHP:

  1. Use the global keyword at the start of every function that uses the variable.
  2. Use the $GLOBALS array.

Of these, I recommend sticking with the second, since it makes it absolutely clear at all times that the variable is a global.

One of the problems with globals is keeping track of where they're being used; using the $GLOBALS array mitigates this issue to some extent.

However, there are still issues with using globals; it's generally considered poor practice to use too many globals in your code. Having worked with a number of legacy systems that used globals extensively, I can vouch for the fact that they can cause a lot of headaches for future developers.

Using globals also makes it harder to write formal test suites for your code (ie unit tests).

My recommendation would therefore be to avoid using globals at all where possible. They are necessary in some cases, but the more you can avoid them, and instead pass variables into your functions and classes rather than making them global, the better things will be.

So to summarise:

If you must use globals, reference them with $GLOBALS['varname'], but it's usually better not to use them at all.

Hope that helps.

like image 181
Spudley Avatar answered Nov 03 '22 05:11

Spudley


For anyone else hitting this old question in a google search,

In the example the variable $second_array was declared a global, not the array created in the following line. To avoid this make sure the global declaration comes after the array declaration. My preference is to put global declaration in the function itself.

$second_array = array();

function operatii($v) {

    global $second_array;  

    $var1 = $second_array[count($second_array)-1];
    $var2 = $second_array[count($second_array)-2];
    $rez = null;

    echo $var1 . $var2 . "este?";
}

for ($i = 0; $i < count($a); $i++){
    if ($a[$i] === "+" || $a[$i] === "-" || $a[$i] === "*" || $a[$i] === "/" ) {
        operatii($a[$i]);
    } else {
        array_push($second_array, $a[$i]);
    }
}
like image 40
Neil Richins Avatar answered Nov 03 '22 07:11

Neil Richins