Posted by Al on 02/03/06 02:02
joe wrote:
> I have a system where include files puts generated HTML into a variable
> ( $output ).
>
> In one of the pages I have a function that executes SQL queries and
> return nice arrays.
>
> The command to abort an included script is 'return' - but not if you're
> within a function. Then 'return' simply aborts that function.
>
> If the query should fail for some reason, I'd like to overwrite $output
> with a nice err. msg. and then abort execution of the included script
> and return to the main script. But can that be done from within a
> function ?
>
> TIA
> ------
>
> Aceb.
In my tests this works as expected, but why not make sure by having the
included file contain a function? Well I know this is awful practise
and might not actually be viable with what you have but if it's just
changing one variable do something like this:
main file:
<?php
echo outputstuff();
function outputstuff() {
include ("include.php");
$output = includefile_function();
echo "still running!";
return $output;
}
?>
and in include.php:
<?php
function includefile_function() {
$error = false;
$HTML = "";
// do normal stuff
if ($error) return "Error!";
// do more stuff
if ($error) return "Error!";
// whatever
return $HTML;
}
?>
That should work, right?
My test was:
main file:
<?php
echo outputstuff();
function outputstuff() {
$output = "this'll be overwritten";
include ("include.php");
echo "still running!";
return $output;
}
?>
and in include.php:
<?php
$error = false;
$HTML = "";
$HTML = "1";
if ($error) { $output = "Error!"; return; }
$HTML = "2";
if ($error) { $output = "Error!"; return; }
// whatever
$output = $HTML;
?>
[Back to original message]
|