473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Returning from an included script while inside a function ?

joe
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.

Feb 2 '06 #1
6 1607
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 ?

Throw an exception?

--
Ian Collins.
Feb 2 '06 #2
joe
Even an exception won't let me return to the main script as far as I
can see

Feb 2 '06 #3
Maybe I'm not understanding your problem correctly, but can't you just
make your function output be an array and make one of those values an
error flag? Then in the main part of the script you could just monitor
the flag value that is returned and wipe out the output at that point
(outside the first function) depending on the condition of the flag?

Feb 2 '06 #4
Al
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;

?>

Feb 3 '06 #5
joe
Yes indeed I could, and that is probably what I'll end up doing.

It just seems like a shame that return can't exit multiple levels
(first exit the function - then exit the include file), because it
would make things simpler. That way I'd only have to write the
errorcheck one place. Now I have to write it several places.

So yeah - it's not a showstopper, only a minor nuisance :)

Feb 3 '06 #6
joe
Thanks for the answer !

If there is a double quotation of your reply - I apologize. I am using
google groups interface, and it appears it won't let me use 'broken'
quotes.
In my tests this works as expected, but why not make sure by having the
included file contain a function?
Yep - I already have a function in the included file :)
Well I know this is awful practise
and might not actually be viable with what you have


Not sure I agree it's an awful practise. I have often used 'utility'
include files that included functions that simplified things I had to
do often.

Also if you do objects - including is a must, otherwise the
practicality of seperating business logic from the objects is lost :)

Your first example would work, but alas it is not viable in my
situation. I am fixing someone elses work, and it's a mess. Totally
sequentially written - then we do this - then we do this. The main file
is index.php and it branches execution to various include files with a
switch statement. It is in fact the only file that is ever called
directly in this system. If I alter the main file - I alter
functionality for a ton of other files, which I'd then have to re-edit
and bugtest.

Right now I am fixing the search_inc.php file, in which various
database calls is scattered all over a mess of 'if(thisorthat) then we
construct this horrible SQL and add that HTML' code. In order for
sanity to prevail, I am arranging things nice and neatly into
functions. I can do this within this include file, but I dare not
venture into any changes that might rock the rest of the system.

Another reason is that the databasefunction is called from other
functions - so I'd have to do an error check after all calls (which is
what I'll end up doing anyway as pr. aschrage16's suggestion) - so
even if I overwrite $output, new content (like end of tables that are
no longer there) will be generated, rendering that whole exercise
obsolete.

At any rate - the problem is not an obstacle, I just thought that since
there IS an 'escape' mechanism for an included file (a return statement
in the global scope), it's odd and a bit annoying that you can't
invoke that mechanism from inside a function.

Feb 3 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mountain Man | last post by:
Hi, I'm having trouble with the foreach function. I'm using it twice inside a user defined function with two different arrays, and in the second instance it's returning the value of the first...
5
by: Nico | last post by:
Hello folks, I am currently storing a set of objects inside an array, $itemlist = array(); $itemlist = new item("myitem"); //... and I am looking to develop a search function, which...
4
by: Deane Barker | last post by:
I have a function that selects a file to include, then includes is. The file is including within the function, like so: function include_file($file_name) { require $file_name; return; }
3
by: jason | last post by:
I've got this javascript routine (i found on google - thank you) in an asp.net page that on page reload sets the cursor of a textbox to the last line. It works great! Using a similar concept, I...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
5
by: J Lake | last post by:
I am working on a simple orderform script to keep a running total, however I am encountering some errors. function CalculateTotal() { var order_total = 0 // Run through all the form fields...
8
by: bidllc | last post by:
I have a funtion that works fine and dandy when called from anywhere in my app. It will NOT work when called from inside the class in which it resides. This is the function I'm calling:...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.