Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Functions Introducing Functions PHP Function Arguments

見綸 陳
見綸 陳
6,333 Points

Why don't global $variable

In previous course video, we write global $current_user in the function to make the function reach outside variable($current_user)

Again, in this video we have a global variable outside the function. WHY DON'T WE CALL THE GOBAL VARIABLE ?

I also do some test to reach the global variable from the function. But the outcome still same. Is there any reason that we don't have to write: global $variable in this case?

        <?php
        function welcome_dialog($user_array) {
            if(is_array($user_array)) {
                global $name;  // ------- I ADD THIS LINE TO TEST -------
                foreach($user_array as $name) {
                    echo "Hello, $name, how are you?</br>"; 
                }
            } else {
                echo 'Hello friends</br>';
            }
        }

        $name = array(
            'Kcin',
            'Sharon',
            'Brother',
            'Sister'
        );

        welcome_dialog($name);

        ?>

1 Answer

Codin - Codesmite
Codin - Codesmite
8,600 Points

It isn't required because the values of $name array are passed into the functions arguments.

<?php
    function welcome_dialog($user_array) {
        if(is_array($user_array)) { 
            foreach($user_array as $name) { 
                echo "Hello, $name, how are you?</br>"; 
            }
        } else {
            echo 'Hello friends</br>';
        }
    }

    $name = array(
        'Kcin',
        'Sharon',
        'Brother',
        'Sister'
    );

    welcome_dialog($name); // Here you are declaring $name array as arguement for the function.

    ?>

In the function where you have foreach($user_array as $name) { It is not calling the global array $name for the values, it is looping the values of $user_array declared in the functions arguments, these are the same values from global $name array as they are declared in the use of the function at the end of the code: welcome_dialog($name);

I've changed the code a bit to make it a bit clearer as to what is happening:

<?php
    function welcome_dialog($user_array) {
        if(is_array($user_array)) {
            foreach($user_array as $canBeAnything) { 
                echo "Hello, $canBeAnything, how are you?</br>"; 
            }
        } else {
            echo 'Hello friends</br>';
        }
    }

    $globalArrayName = array(
        'Kcin',
        'Sharon',
        'Brother',
        'Sister'
    );

    welcome_dialog($globalArrayName);

    ?>

Here are some more simplified examples:

<?php
// This requires global

    $x = 75;
    $y = 25;

    function addition() {
        global $x;
        global $y;
        $z = $x + $y;
    }

    addition();
    echo $z;
?> 
<?php
// This does not requires global as the values of $x and $y are called as arguements for the addition function

    $x = 75;
    $y = 25;

    function addition($x,$y) {
        $z = $x + $y;
    }

    addition($x,$y);
    echo $z;
?> 

Hope this helped explain :)

見綸 陳
見綸 陳
6,333 Points

Pretty clear, thanks a lot!

Andrew Dovganyuk
Andrew Dovganyuk
10,633 Points

the echo $z; must be inside the function to display z