Solved

Goto/label

Posted on 2001-09-07
4
568 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now