tags:

views:

68

answers:

3

I'm using PHP's global declaration to make an array available to a number of functions in a script. The variable is declared at the top of the script and is referenced with global in each of the functions which uses it, as follows:

<?php

$myarray = array(1, 2, 3);

function print_my_array() {
    global $myarray;

    print '<ul>';
    foreach($myarray as $entry) {
        print '<li>'.$entry.'</li>';
    }
    print '</ul>';  

    return 0;
}

print_my_array();

?>

Sometimes, but not always, the array is not set when the function is called, generating an error when the foreach is called. In the actual code, the array used is given a very unique name and so should not be causing any collisions with anything else. Am I mis-using the global declaration?

+3  A: 

No, the snippet is correct. The problem you're having is the problem of using global variables – they can be accessed and changed from anywhere (perhaps accidental), thereby creating hard-to-find bugs.

Artefacto
+1 Preach on, brother
John Conde
In general I'd agree with you, but this 'global' variable was really only used in one script in one spot. Why not just define it there? It needed to be defined in a visible spot, at the top of the script, but it's used in a spot buried way down the page. There was zero chance that it was being used or accessed anywhere else, so I still consider my question to be unanswered.
S McKinley
Again, your snippet is correct. The only way the array could be null sometimes is if there's something happening in the interim between the definition and any of the calls to `print_my_array`.
Artefacto
A: 

The least you could do is check if the array is null at the top of the function, before you run the foreach. that would at least prevent the error:

function print_my_array() {
    global $myarray;

    if(!empty($myarray)) {
        print '<ul>';
        foreach($myarray as $entry) {
            print '<li>'.$entry.'</li>';
        }
        print '</ul>';  
    }
}

Also, I wouldn't just return 0 for the hell of it. You may want to incorporate whether or not the array was empty into what you return from this function.

jordanstephens
This is a sensible thing to do, but I NEED that array to be defined in order for the rest of the script to work. Not having it defined is not an option.
S McKinley
+1  A: 

By using globals you can hit quite a few gotchas, they'll also make you code less reusable.

Here's an example of your function which can be re-used many times across the site.

(untested)

<?php
function arrayTags($items, $open = '<li>', $close = '</li>')
{
    if (is_array($items) && count($items) != 0)
    {
        $output =   null;

        foreach ($items as $item) {
            $output .= $open . $item . $close;
        }

        return $output;
    }
    else
    {
        return '';
    }
}

// Default, <li>
echo '<ul>' . arrayTags($myarray) . '</ul>';

// or, spans:
echo '<div id="container">' . arrayTags($myarray, '<span>', '</span>') . '</div>';
Kieran Allen