Back to Top

GoTo statement in PHP


Recently I have used Goto and PHP supports goto statements to branch unconditionally from one point to another in the program. Goto statement is only available in latest version of PHP and its PHP 5.3 or higher PHP versions.We can say Goto statement is used to jump to other section of the program. I have also written about the abstract method in oops which is available on PHP 5.3.

What is GoTo?

The Goto Statement requires a label to identify the place where the branch to be made. A label is any valid variable name and must be followed by a colon(:). A label is immediately placed before the statement where the control is to be transferred.

The GoTo Statement is used to jump to another part of the Program and skip that part of the loop.GoTo Statement exits the loop as soon as the accurate condition is satisfied.The common use of the GoTo Statement is better to use it instead of multilevel break Statement.

GoTo Statement transfers the control to any place in a program and it creates branch within the loop.PHP GoTO Statement allows to jump from one statement to another within a loop as well as jump out of the loop.

The general form of Goto:


NOTES: The label: can be in anywhere in the program before or after the goto label; statement.

When program run and get the statement like

go to first;

The flow of program will jump to the statement first: This occurs unconditionally.

NOTES:goto is not a function. Its just a statement.

Let’s take a quick look at an example:


echo "Lets start with Go To"." ";
goto read;
echo "Skip the code with Goto jump";
echo "Execute the code with Goto jump";


Let’s start with Go To
Execute the code with Goto jump

In the above example, when the code encounters the goto statement, the control transfer to the label “read”. The important use of GoTo Statement is to exit from deeply nested loops when any error occurs.

Also Read:
Five Reasons Why You Should Go With PHP
PHP Magical Constants
How to use Namespace in PHP

Last but not least

“Don’t go to GO TO statement”.Try to avoid the use of goto as far as possible but there is nothing wrong if we use it to improve execution speed of the program.

It is a better to avoid using GoTo Statement and there are many reasons for this.GoTo makes a logic of program more complicated and renders the program unreadable form.So, It is better to avoid using GoTo Statement to program design carefully.In case, GoTo statement is absolutely necessary for you to use, It should be well documented otherwise cause problems.

Do you use Go To Statement in PHP? If you are using GoTO Statement in PHP, do let me know via comments and share your experience.

Comments (29)

  1. can we jump out of any function using goto?

  2. No we can’t jump out of any function with goto

    1. Slam dunkin like Shaqulile O’Neal, if he wrote informative articles.

      1. Alright alright alright that’s eacxlty what I needed!

      2. this is what im lnooikg for almost half day. have try many other tutorial but no luck. i will try my best to follow this tutorial and give a feedback about the result. thank you!

  3. So, basically goto statement use to skip the code or statement ?. And if we can’t jump any other function then what is other use of goto ?

    1. Goto can be very useful if you want to jump out of multiple loops. with the break you have to break out of each loop but with goto you can jump out of multiple loops..

      < ?php for($i=0;$i<10;$i++) { for($j=0;$j<10;$j++) { echo $i.'-'.$j.'
      // break;
      goto lable;
      echo $i.’

    2. I found just what I was nedeed, and it was entertaining!

      1. Whoa, whoa, get out the way with that good infmoratoin.

  4. goto jumps to bottom of program.. is there any function or statement which can jump to upper part of code…

    1. yes you can use goto to jump upper part of the code like

      goto label;

      and its called “backward jump”

      1. Most help articles on the web are inaccurate or inoechernt. Not this!

      2. Yup, that shloud defo do the trick!

    2. This has made my day. I wish all ptsoings were this good.

      1. The genius store caleld, they’re running out of you.

  5. kirk bushell says:

    Bhumi – you can use something like break 2; if you want to break out of 2 loops.

    I can’t stand goto – and was kind of glad that languages moved away from it. Now that PHP is implementing it, it is yet another reason as to why I move further and further away from the language.

    1. Yes we can use break but we need to add break two times(one break for inner loop and one break for outer loop) to out of 2 loops. whenever we need single goto statement to out of multiple loops

      1. I didn’t know where to find this info then kaobom it was here.

      2. You got to push it-this esnsetial info that is!

  6. I feel satisfied after redaing that one.

  7. […] that can be used to repeat code until the LOOP is manually exited using a LEAVE statement like a goto […]

  8. Interesting post. It’s very good to learn more about this subject.

  9. Usually I do not learn post on blogs, however I wish to say that this write-up very compelled me to
    take a look at and do it! Your writing taste has been surprised me.
    Thank you, quite great article.

  10. Thanks for another informative website. Where else may just
    I am getting that kind of info written in such a perfect method?

    I’ve a project that I am just now operating on, and I have been on the look out for
    such info.

  11. Hi Bhumi, Thanks for your information in this article…

  12. very useful article, thanks for sharing

  13. Nice article, thanks for information. 🙂

  14. Every weekend I used to pay a quick visit
    this web site, because I want enjoyment, as this site conations truly
    nice data too.

  15. As a developer, I would always hate to use goto statement. I do not think this is a good way of thinking for developers.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to set Module Position in Joomla

Posted on 7 years ago


How to Animate SVG Image with example

Posted on 4 years ago


Hello world

Updated 1 month ago

Nick Carter

In depth Understanding of Laravel Core

Updated 3 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – June 2015

Posted on 4 years ago


The Reader’s Poll – August 2014

Posted on 5 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 6 years ago