Re: static vars question

From: Date: Fri, 30 May 2003 21:16:16 +0000
Subject: Re: static vars question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-149599@lists.php.net to get a copy of this message
At 5/30/2003 04:49 PM, Martin Helie wrote:
function test() {
    static $i = 0;
    if( $i < 10 ) {
        $i++;
        test();
    }
    echo "I'm here";
} I am a little surprised to find that even when $i < 10 and test() is invoked again, the current function call executes all the way through to echoing "I'm here".
Why? The "I'm here" line is outside the conditional. The function will still complete independent of the conditional. If $i is greater than ten, the pointer just skips the contents of the if/then and continues on the next line after it. Here's a better illustration: function gooble($alGore) { echo "This is a "; if ($alGore!="President") { echo "longer "; } echo "sentence.<br />"; } gooble("Vice President"); gooble("Crazy Mountain Man); gooble("President"); If you want the function to terminate when the conditional is false, then you need an Else statement to do so, otherwise it's going to keep going. -- S. Keller UI Engineer The Health TV Channel, Inc. (a non - profit organization) 3820 Lake Otis Pkwy. Anchorage, AK 99508 907.770.6200 ext.220 907.336.6205 (fax) Email: skeller@healthtvchannel.org Web: www.healthtvchannel.org

« previous php.general (#149599) next »