i have a function

lets say
function myfunc()
{
echo 'hi';
}

now the code may call the function myfunc more than a time
but there should be only one time echo of 'hi'

pliz help anything could work .... but all my effort in vain
static variable ????
global variable checking ???
code in a separate file n making include_once ?????


pliz help

Dani AI

Generated

Short answer: keep the "run once" check inside the function. That way every caller can call display() and the function itself prevents the one-time portion from repeating. This works per PHP request (it does not persist across separate HTTP requests unless you explicitly store state).

A simple, robust pattern is a function-level static flag:

function display()
{
    static $ran = false;

    if (! $ran) {
        // one-time work goes here (initial output, initialization, etc.)
        $ran = true;
    }

    // code that should run on every call
}

An alternative is a constant check if you prefer a global, immutable marker:

if (! defined('DISPLAY_DONE')) {
    // one-time work
    define('DISPLAY_DONE', true);
}

Troubleshooting notes: pointed out guarding at the call site, but since you cannot change callers that is not practical here. Common reasons a static/global guard appears to "not work": the variable is reinitialized each call (not declared static), global was forgotten inside the function, the file holding the function is being included via different paths so include_once is ineffective, or you expect persistence across requests (static/constant only lasts for the current PHP run). To avoid include path issues use a single canonical include (for example via an absolute path), or centralize the function in one bootstrap file so it is guaranteed loaded once.

<?php 
function display()
{
    echo "hello<br>";
}
while($i<=5)
{
    display();
    $i++;
}
?>//save this file and include this file in other file using include"filename.php";

i said just a one ...

yes i will be call the function many time from different-different modules

But certain codes inside the function should execute just a once ..... not twice .. not five times....

thank you

If you only want it to show once just use something like

...
if($has_not_displayed)
{
  display();
  $has_not_displayed = FALSE;
}
...

:(

ok let me little clearly state my problem much in detail

there are 50 file that each contains display() function at-least at 10 diff places.


display function is in display.php (included by all 50 files)

now i need single execution of certain contains of display()..... i cant make conditions while calling the function .... i need to do some thing in common inside that function ...


Thanks for follow up ...
i know ... this could be done
through static var or globals or include_once
but i dont know y all of these are not working with me this time ....

i m little bg ... i will specify the code ...
i know somewhere i got mistake ... but i m not getting for identifying it ...
thank you once again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.