Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP and the goto statement to be added in PHP 5.3

Tags:

php

goto

The "goto" statement comes straight out of ASM or any other assembler language.

Here's a link: http://be2.php.net/manual/en/control-structures.goto.php

I'm wondering: what can this do to make my code more well-organized? How can I implement this in larger projects, without screwing it up. Since the goto will allow you to jump back and forth, accidental assignments and infinite loops are waiting to happen if you use this the wrong way.

Can someone give me an example of a good use of this?

EDIT: allright, I've seen some of the replies and apparently a wide consensus exists about the use of the "goto" statement and it being bad.

So I'm still wondering: why would PHP bother to add it to the language. If they didn't see something in it, they wouldn't do it... so why?

Also: A discussion here on StackOverflow about "goto" in general

EDIT2: Seeing as this question induced a lot of bad things to be sad about the goto statement, I went and asked my father. He's 52 years old and is an Industrial Engineer.
He told me a couple of times he did a good amount of programming in his days and mostly in FORTRAN and COBOL. Nowadays he does IT services, server&networkmanagment and such.

Anyways, he said some stuff about "back in my day..."
After discussing that a bit, he came back to the goto statement, saying that even back in his days as a student, they allready knew it wasn't a smart idea to use it, but they didn't have much better back then. Try/catch was still years away and error handling hardly excisted.
So what did you do to check your program? Add a few lines at the end that allow you to print output and everything you need to check in your code, and then you place the line: "goto printing;", or something like that, to start the printing of your data.

And in this manner, you gradually debugged your code.

He agrees that the use of goto in the modern programming world is pretty useless. The only use he finds justified is an "emergency break", to be used in extreme debugging and unexpected situations. Kinda like goto fatal_error;, and have the "fatal_error" part of your code do some things to show you in-depth results.
But only during the creation of something. A finished product should not have goto-statements.

LATE EDIT: Another discussion about "goto" in PHP5.3/PHP6

like image 379
KdgDev Avatar asked Apr 06 '09 21:04

KdgDev


People also ask

Does PHP have a goto statement?

The Goto statement is only available in the latest version of PHP 5.3 or higher PHP versions. We can say that a Goto statement jumps to another section of the program. The Goto Statement requires a label to identify the place where the branch is to be made.

How do I use goto in PHP?

goto ¶ The goto operator can be used to jump to another section in the program. The target point is specified by a case-sensitive label followed by a colon, and the instruction is given as goto followed by the desired target label.

How do you implement a goto statement?

Syntax of goto Statementgoto label; ... .. ... ... .. ... label: statement; The label is an identifier. When the goto statement is encountered, the control of the program jumps to label: and starts executing the code.

How jump to another line in PHP?

Using new line tags: Newline characters \n or \r\n can be used to create a new line inside the source code. Example 1: PHP.


2 Answers

If you're writing good PHP code, you shouldn't need to use goto. I think it's a mistake that they're adding it in, as it just leads to lazy programming.

See

http://www.procata.com/blog/archives/2004/07/29/goto-in-php/

For a good commentary on the addition of this to PHP, and also, here on stack overflow,

GOTO still considered harmful?

like image 121
Mez Avatar answered Oct 17 '22 07:10

Mez


I have only ever found two uses for goto:

  1. To break out of nested loops. But most newer languages have a mechanism to do this without goto anyway (break <number> in PHP, or break <loop label> in Java, etc.).
  2. To go to a cleanup section at the end of a function. But again, this isn't often useful in a garbage-collected language.

In other words, if you don't know whether you should use goto for something, you shouldn't.

like image 21
Michael Myers Avatar answered Oct 17 '22 07:10

Michael Myers