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:

PHP Goto general form statement

.
Important Note: The label: can be anywhere in the program before or after the Goto label; statement.
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:

statement1;
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

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

x=1 x=2 x=3 x=4 x=5 x=6 x=7 x=8 x=9 x=10

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

Fatal error: ‘goto’ into loop or switch statement is disallowed in C:\wamp64\www\index.php on line 5
Example:6

Let us we start with Go To 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!