Page 1 of 1

how can i call function only once in the page

Posted: Thu Sep 07, 2006 1:20 am
by B.Murali Krishna
hello,
here i have a application that, i need to call the function only once, during the execution of the program,
please help me

Regards
Murali

Posted: Thu Sep 07, 2006 1:27 am
by Luke
you need to only call it once, or you need to ensure that it can only be called once... ?

Posted: Thu Sep 07, 2006 2:25 am
by Chris Corbyn
Put a static variable in it and set it true.

Code: Select all

function echoOnce($word)
{
    static $has_run = null;
    if ($has_run) return;

    echo $word;
    $has_run = 1;
}

echoOnce("Foo");
echoOnce("Bar");

Re: how can i call function only once in the page

Posted: Thu Sep 07, 2006 2:47 am
by jmut
B.Murali Krishna wrote:hello,
here i have a application that, i need to call the function only once, during the execution of the program,
please help me

Regards
Murali
I would suggest your reconsider the workflow. While it is possible you have such weird situation it is more likely you don't need this behaviour at all.
Fix design up front to have less trouble/mess later.