Back to Top

GoTo statement in PHP

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:

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:

Example:

Output:

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 (39)

  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..

      ‘;
      // break;
      goto lable;
      }
      echo $i.’
      ‘;
      }
      lable:
      die;
      ?>

    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

      label:
      statement;
      ——-
      ——-
      ——-
      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. 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.

  16. Only wanna remark that you have a very nice web site, I the pattern it really stands out.

  17. You got a very fantastic website, Glad I discovered іt tһrough yahoo.

  18. Hi there, just wanted to mention, I loved this blog post.

    It was inspiring. Keep on posting!

  19. We absolutely love your blog and find a lot of your post’s to be precisely what I’m looking for.
    Do you offer guest writers to write content in your case?
    I wouldn’t mind composing a post or elaborating on a number of
    the subjects you write in relation to here. Again, awesome weblog!

  20. I read this post fully concerning the resemblance of most up-to-date and previous technologies, it’s amazing article.

  21. I was recommended this web site by my cousin.
    I’m now not sure whether this post is written via him as no one else
    know such special about my difficulty. You’re amazing! Thanks!

  22. If some one wants to be updated with most recent technologies afterward he must
    be pay a visit this web page and be up to date daily.

  23. This is a great tip especially to those new to the blogosphere.
    Simple but very accurate information… Many thanks for sharing this one.

    A must read article!

  24. I have read several good stuff here. Certainly worth bookmarking for
    revisiting. I surprise how so much attempt you place to make such a excellent informative website.

  25. Terrific post however , I was wanting to know if you could write a litte more on this subject?
    I’d be very thankful if you could elaborate a little bit further.
    Many thanks!

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

The Readers’ Poll – August 2013

Posted on 11 years ago

Bhumi

How to Read an RSS Feed With PHP

Posted on 12 years ago

Bhumi

What are Lambdas and Closures in PHP?

Posted on 7 years ago

Bhumi