Wednesday 14 September 2011

IT

Using Static Variables


 Problem

You want a local variable to retain its value between invocations of a function.

Solution

Declare the variable as static:
function track_times_called( ) {
    static $i = 0;
    $i++;
    return $i;
}

No comments:

Post a Comment