Jump to content

Prevent function from outputting markup more than once


LostKobrakai
 Share

Recommended Posts

Hi,

I've a function which outputs gallery markup. This gallery can be included in the text via a hanna code or should be placed below the other content. Somehow just setting a variable at the start of template rendering and use "global" to change it doesn't work.

// _init.php
$gallery_used = false;

// _func.php
function renderGallery(){
	global $gallery_used;

	if(!$gallery_used) return "gallery";
	else return "already used";

	$gallery_used = true;
}
Link to comment
Share on other sites


// _init.php

$GLOBALS['gallery_used'] = false;

// _func.php

function renderGallery(){

if(!$GLOBALS['gallery_used']) {

     $GLOBALS['gallery_used'] = true;

return "gallery";

} else {

return "already used";

}

}

Edited by horst
moved a line into the right direction :)
  • Like 1
Link to comment
Share on other sites

// _init.php
$GLOBALS['gallery_used'] = false;

// _func.php
function renderGallery(){
	if(!$GLOBALS['gallery_used']) {
	        $GLOBALS['gallery_used'] = true;
                return "gallery";
        } else {
                return "already used";
        }
}

With the exception of an error in the order your solution does work horst, thank you both.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...