function pleac_Making_Variables_Private_to_a_Function()
{
    // Strictly-speaking, PHP is neither lexically [no environment capture] nor
    // dynamically [no variable shadowing] scoped. A script in which several
    // functions have been defined has two, entirely separate, scopes:
    //
    // * A 'top-level' scope i.e. everything outside each function
    //
    // * A 'local scope' within each function; each function is a self-contained
    //   entity and cannot [via conventional means] access variables outside its
    //   local scope. Accessing a variable that has not been locally defined
    //   serves to define it i.e. accessing a variable assumed to be global
    //   sees a local variable of that name defined
    //
    // The way 'global' variables are provided is via a predefined array of
    // variable names, $GLOBALS [it is one of a special set of variables known
    // as 'superglobals'; such variables *are* accessable in all scopes]. Each
    // entry in this array is a 'global' variable name, and may be freely
    // accessed / updated. A more convenient means of accessing such variables
    // is via the 'global' keyword: one or more variables within a function is
    // declared 'global', and those names are then taken to refer to entries
    // in the $GLOBALS array rather than seeing local variables of that name
    // accessed or defined
    function some_func()
    {
        // Variables declared within a function are local to that function
        $variable = 'something';
    }
    // ----------------------------
    // Top-level declared variables
    $name = $argv[1];
    $age = $argv[2];
    $c = fetch_time();
    $condition = 0;
    // ------------
    function run_check()
    {
        // The globally-declared variable, '$condition', is not accessable within
        // the function unless it declared as 'global. Had this not been done then
        // attempts to access, '$condition', would have seen a local variable
        // of that name declared and updated. Same applies to other variables
        global $condition, $name, $age, $c;
        $condition = 1;
        // ...
    }
    function check_x($x)
    {
        $y = 'whatever';
        // This function only has access to the parameter, '$x', and the locally
        // declared variable, '$y'.
        // Whilst 'run_check' has access to several global variables, the current
        // function does not. For it to access the global variable, '$condition',
        // it must be declared 'global'
        run_check();
        global $condition;
        // 'run_check' will have updated, '$condition', and since it has been
        // declared 'global' here, it is accessable
        if ($condition) {
            // ...
        }
    }
}
Example #2
0
function store_data()
{
    $cookie = fetch_cookie();
    # Validated for certain safety measures.
    $ua_id = get_ua();
    $tokens = param_val("tokens", "/^[:_a-zA-Z0-9 ,-]+\$/");
    global $dbhandle;
    $sql = sprintf("REPLACE INTO survey (\n      status_a, \n      status_aaaa,\n      status_ds4,\n      status_ds6,\n      status_ipv4,\n      status_ipv6,\n      status_v6ns,\n      status_v6mtu,\n      status_dsmtu,\n      \n      time_a,\n      time_aaaa,\n      time_ds4,\n      time_ds6,\n      time_ipv4,\n      time_ipv6,\n      time_v6ns,\n      time_v6mtu,\n      time_dsmtu,\n      \n      tokens,\n      ua_id,\n      \n      cookie, ip, ip4, ip6\n      \n      )\n     VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s',\n             '%s','%s','%s','%s','%s','%s','%s','%s','%s',\n             '%s','%s',\n             '%s','%s','%s','%s')", mysql_real_escape_string(fetch_status("a"), $dbhandle), mysql_real_escape_string(fetch_status("aaaa"), $dbhandle), mysql_real_escape_string(fetch_status("ds4"), $dbhandle), mysql_real_escape_string(fetch_status("ds6"), $dbhandle), mysql_real_escape_string(fetch_status("ipv4"), $dbhandle), mysql_real_escape_string(fetch_status("ipv6"), $dbhandle), mysql_real_escape_string(fetch_status("v6ns"), $dbhandle), mysql_real_escape_string(fetch_status("v6mtu"), $dbhandle), mysql_real_escape_string(fetch_status("dsmtu"), $dbhandle), mysql_real_escape_string(fetch_time("a"), $dbhandle), mysql_real_escape_string(fetch_time("aaaa"), $dbhandle), mysql_real_escape_string(fetch_time("ds4"), $dbhandle), mysql_real_escape_string(fetch_time("ds6"), $dbhandle), mysql_real_escape_string(fetch_time("ipv4"), $dbhandle), mysql_real_escape_string(fetch_time("ipv6"), $dbhandle), mysql_real_escape_string(fetch_time("v6ns"), $dbhandle), mysql_real_escape_string(fetch_time("v6mtu"), $dbhandle), mysql_real_escape_string(fetch_time("dsmtu"), $dbhandle), mysql_real_escape_string($tokens, $dbhandle), mysql_real_escape_string($ua_id, $dbhandle), mysql_real_escape_string($cookie, $dbhandle), mysql_real_escape_string(remote_addr(), $dbhandle), mysql_real_escape_string(fetch_addr("ip4"), $dbhandle), mysql_real_escape_string(fetch_addr("ip6"), $dbhandle));
    #        print $sql;
    $result = mysql_query($sql, $dbhandle);
    if (!$result) {
        $message = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $sql;
        die($message);
    }
    #  header("HTTP/1.0 204 No Content");
}