Solved

Goto/label

Posted on 2001-09-07
4
575 Views
Last Modified: 2012-05-04
Is there such a function like goto in PHP

I mean.. instead of calling exit() at many place in my code.. I'd like to send all 'exit()' to a single place where I could do the appropriate action (closing the DB)
0
Comment
Question by:higijj
  • 3
4 Comments
 
LVL 4

Expert Comment

by:heddesheimer
ID: 6466480
Try die() with a function name as parameter to clean up things:
http://www.php.net/manual/en/function.die.php

See Example 2 for using die with a function as parameter

Marian
0
 
LVL 2

Author Comment

by:higijj
ID: 6468941
ok.. but what if I don't want my script to die?
0
 
LVL 4

Expert Comment

by:heddesheimer
ID: 6469723
then try another question :-)

You asked in your original question 'I'd like to send all 'exit()' to a single place where I could do the appropriate action (closing the DB)' and that is what I told you.

If you want to continue the script you have to use if, while or switch

Marian
0
 
LVL 4

Accepted Solution

by:
heddesheimer earned 50 total points
ID: 6469777
Maybe it is not perfectly clear to you so I will explain in detail: exit() will cause the program to end as well as die() do. The advantage of die() with a function parameter is, that you can perform some cleanup on your script before it will end.

Ending or die()-ing a script does not mean that the user will see an error message, it will just end the page as a "normal" ending of your script will do anyway. If you use die() with a cleanup function, you should consider to print the missing ending tags (i.e. </body></html>) to generate valid html code when you exit your script.

What comes closest to a goto like you need it is a do-while with break:

<?
$test = false;
do
{
  echo "hello<br>";
  if (!$test)  break;
  echo "hallo again<br>";
} while (false);
echo "cleanup<br>";
?>

depending on the value of $test the code will go thru the while loop only once or it will break out at the if($test) condition. You can have several break statements in that loop which means (go-to the end of the do-block).

Try this code out with $test=false and $test=true to see the result.

There is (fortunately) no go-to statement implemented in php, so you need to build at lest a minimal logical structure to accomplish what you need.

Hope that will make things clearer

Marian
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question