?
Solved

Goto/label

Posted on 2001-09-07
4
Medium Priority
?
579 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 150 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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.
Suggested Courses

771 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