/    Sign up×
Community /Pin to ProfileBookmark

"Double return" from function in included file?

Hey all,

I was wondering if anyone had any ideas about this conundrum. I’m executing file A, in which I include and execute a (slightly arbitrary) PHTML file B. File B calls a (global) function C. After C executes (i.e. inside C), I want to skip executing the rest of file B. However, calling [COLOR=Red]return[/COLOR] inside the function won’t work because that’ll just end the function, not skip the rest of the file. Calling [COLOR=Red]exit[/COLOR] also won’t work, because that’ll ALSO skip the rest of file A, which I don’t want to do. Essentially I need a “double return” – return from the function, then return from the file. Can I do this from inside the function at all, or will I have to make sure to do this from the individual file?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 16.2006 — You can use return() within the body of an include script, so I suppose you could create some sort of conditional statement that would terminate the include at the desired point, perhaps based on some global variable you'd set in the calling script before doing the include statement.

However, my suggestion (to avoid maintenance headaches in the future) would be to rethink the include strategy. Possibly you could break out the part of the include file that you only want to use in this case, and make it a separate file. Generally, however, my include files consist entirely of functions and/or class definitions. So I include the file at the start of a script, then call the desired functions if/when needed.
Copy linkTweet thisAlerts:
@CidolfasauthorOct 16.2006 — Well... it's not actually an include file, but an eval() statement (I used includes as my example to simplify things). What's more, I don't actually have direct control over which file is being executed. I can alter it before executing, though, so I might actually have to search for the function call and insert a "return" statement after it before executing, if no one can come up with something better. ^^;
Copy linkTweet thisAlerts:
@ShmohelOct 16.2006 — What I do when various parts of my script need to return values is something like below:

[code=php]
#do something here.....
if($foo=="bar"){
$return.="hello";
}
#continue executing stuff here.....
foreach($someArray as $val){
$return.=$val;
}

return $return;
}[/code]


Basically... stick all that needs to get returned into one varaible, and then return it at the end.
Copy linkTweet thisAlerts:
@CidolfasauthorOct 16.2006 — My problem isn't getting a value, it's stopping execution at a particular point, i.e. skipping the rest of an included file - from a function - without skipping the rest of the file that included it.
Copy linkTweet thisAlerts:
@bokehOct 16.2006 — Read the file (file_get_contents), use preg to capture the piece of code you want and then eval it. Personally I think Nogdog is right; rethink your code.
Copy linkTweet thisAlerts:
@chazzyOct 17.2006 — My problem isn't getting a value, it's stopping execution at a particular point, i.e. skipping the rest of an included file - from a function - without skipping the rest of the file that included it.[/QUOTE]

I'm not sure what you mean

let's say function a is in include file include.php

[code=php]function a(){
//do stuff
return "a value";
}[/code]


and let's say page b.php includes this, and calls function a

[code=php]
include include.php

$retval = a();
//do more stuff
echo $retval;
[/code]


I'm not sure what's up with your code, why you think it's stopping execution. It's possible that you an error in your code, in the page "b.php" like mine, but since PHP is parser based it should still execute the included page.
Copy linkTweet thisAlerts:
@bokehOct 17.2006 — I'm not sure what you mean.[/QUOTE]He means the include looks like this:[code=php]function DoStuff()
{
# do something
}

echo $variable;[/code]
And that he doesn't want to echo the variable, but doesn't want the script to abort has he wishes to continue with the code in the calling file that exists after the include request. My advice is change the logic. Keep functions and procedural code separate.
×

Success!

Help @Cidolfas spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...