PHP Goto | How to use Goto Statement in PHP with Example?
Summary: In this article, you will learn, PHP Goto statement, and how to use Goto in PHP, let’s understand in details with example. Goto statement is only available in the latest version of PHP and its PHP 5.3 or higher PHP versions. We can say Goto statement is used to jump to other sections of the program.
What is PHP goto statement?
- The Goto Statement needs a label to identify the place where the branch to be made. A label is any valid variable name and should be followed by a colon(:). A label is instantly placed before the statement where the control is to be transferred.
- It is used to jump to another part of the Program and skip that part of the loop. furthermore, GoTo Statement exits the loop as soon as the correct condition is satisfied. Therefore, the common use of the GoTo Statement is better to use it rather than of multilevel break Statement.
- PHP GoTo Statement transfers the control to any place in a program and it creates a branch within the loop. It allows to jump from one statement to another within a loop as well as jump out of the loop.
The general form of PHP Goto:
Goto is not a function. It’s just a statement.
The flow of the program will jump to the statement first: This occurs unconditionally.
The goto statement is used to send the flow of the program to a certain location in the code. The location is specified by a user-defined label. Generally, goto statement comes in the script as a part of conditional expression such as if, else, or case (in switch construct)
Syntax:
statement2;
if (expression)
goto label1;
statement3;
label1: statement4;
EXPLANATION: After statement2, if the expression (as a part of the if statement) is true, program flow is directed to label1. If it isn’t true, then statement3 will get executed. The program continues in normal flow afterward.
In the following example, If the number input by the user is even, the program jumps to the specified label
PHP Goto Example:
Example:1
1 2 3 4 5 6 7 8 9 |
<?php $a=(int)readline("enter a number"); if ($a%2==0) goto abcde; echo "a is an odd number"; return; abcde: echo "a is an even number"; ?> |
The label in front of the goto keyword can appear before or after the current statement. If the label in the goto statement identifies an earlier statement, it constitutes a loop.
The following example shows a loop constructed with a goto statement
Example:2
1 2 3 4 5 6 7 8 |
<?php $a=0; start: $a++; echo "x=$a\n"; if ($a<10) goto start; ?> |
Using PHP goto statement, program control can jump to any named location. However, jumping in the middle of a loop is not allowed.
Example:5
1 2 3 4 5 6 7 8 9 10 |
<?php for ($a=1; $a<=10; $a++){ if (a==5) goto inloop; for ($y=1;$y<=10; $y++){ inloop: echo "x=$a y=$y\n"; } } ?> |
Example:6
1 2 3 4 5 6 7 |
<?php echo "Let us we start with Go To"." "; goto read; echo "To Skip the code with Goto jump"; read: echo "then, execute the code with Goto jump"; ?> |
In the above program example, when the code encounters the goto statement, the control transfer to the label “read”. The important use of the GoTo Statement is to exit from deeply nested loops when an error occurs.
Conclusion:
“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 the execution speed of the program.
It is better to avoid using GoTo Statement and there are many reasons for this. GoTo makes the logic of the program more complicated and renders the program unreadable form. So, It is better to avoid utilized the 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.
In this article, you have learned PHP Goto statement and also learned how to use Goto Statement in PHP with Example. I hope you will enjoy it!. if have any query then contact on info@tutorialscan.com I will try to resolve the problem.
Stay healthy, stay safe! Happy coding! enjoy it!