Solved

Goto/label

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

809 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