Jump to: navigation, search

Php goto

From w3cyberlearnings

Contents

goto

goto jump to another section of the program.

  • goto can not use to jump out of the function or method
  • goto must be within the same block or section of the program
  • goto can not jump to loops or switch structure

Syntax

goto A3;
echo 'Now here';
A3:
  echo 'A3 is hear';

NOTE

By using goto, the program will be difficult to maintain. If you must you goto, and it is only your option than use it.

Example 1

<?php

goto M;
echo 'Hello World!';

M:
echo 'great M';

?>

Output

great M

Example 2: goto within the while loop

  • This is for demonstratio only, if you can use other methods than goto, please use so.
<?php

$n = 8;

while ($n-- > 0) {
	if ($n % 2 == 0) {
		goto n_mod_2_0;
	} else {
		goto n_mod_2_not_0;
	}
	n_mod_2_0: {
		echo $n . ' % 2==0 <br/>';
	}
	n_mod_2_not_0: {
		echo $n . ' % 2 != 0<br/>';
	}
}
?>


Output

7 % 2 != 0
6 % 2==0 
6 % 2 != 0
5 % 2 != 0
4 % 2==0 
4 % 2 != 0
3 % 2 != 0
2 % 2==0 
2 % 2 != 0
1 % 2 != 0
0 % 2==0 
0 % 2 != 0


Related Links


Navigation
Web
SQL
MISC
References