php function variable scope

To answer literal question:

// Global variable
$admin_arr = array(1, 2);

function isAdmin ($user_id) {

    // Declare global
    global $admin_arr;

    foreach ($admin_arr as $value) {

        if ($value == $user_id) {
            return true;
        }
    }

return false;
}

Documentation here: http://php.net/manual/en/language.variables.scope.php

To answer the REAL question: Avoid global at all costs. You are introducing a plethora of error prone code into your application. Relying on global variables is entering a world of pain and makes your functions less useful.

Avoid it unless you absolutely see no other way.

Leave a Comment