这非常简单,您只需要:
确保某个变量(通常是布尔值)指示执行某个函数的结果。这是非常通用的方法。
因此,在程序代码中,它看起来像这样:
<?php
//initial state;
$already_printed = false;
/**
* instead of using a global keyword, use reference
* it makes code a bit "clean"
*/
function print_div_boxes(&$already_printed) //<-- we are passing a reference, not a copy
{
echo 'Print (large sum of) data & divs....';
//A couple of thing to note:
//1) Function Return value SHOULD NOT BE RESULT OF ASSIGNMENT!!!
//2) No 'return' keyword actually required here, its VOID
//3) You expect a logic, so use boolean instead of int. It's more appropriate
$already_printed = true;
}
//Then use it like:
if ( $already_printed === false ){
print_div_boxes(&$already_printed);
}