The problem is that whenever you call flag_it()
there’s no $flag
available, so it’s always going to result in “flag worked”. Try using a static variable instead:
function flag_it() {
static $flag;
if ( isset( $flag ) )
return;
$flag = true;
echo 'Flag worked!';
}
Note that no matter how many times you call this function in a run, it will always echo just once. If you want to reset the counter you’ll need to change it to accept some sort of $reset
argument.
You can read more about variable scopes in PHP here: http://php.net/manual/en/language.variables.scope.php