Solved

Goto/label

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

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.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

695 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