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"; read: 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.
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.