how can i call function only once in the page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
B.Murali Krishna
Forum Commoner
Posts: 28
Joined: Fri May 12, 2006 2:05 am
Location: Hyderabad,India
Contact:

how can i call function only once in the page

Post 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

you need to only call it once, or you need to ensure that it can only be called once... ?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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");
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

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

Post 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.
Post Reply