PHP and goto statement to add in PHP 5.3

The goto operation is derived directly from ASM or any other assembly language.

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

I am wondering: what can this do to make my code more organized? How can I realize this in large projects without screwing it. Since goto allows you to jump back and forth, random assignments and endless loops are waiting if you use it incorrectly.

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

EDIT: OK, I saw some answers and there seems to be wide consensus on using the goto statement, and that is bad.

So I'm still wondering why PHP wants to add it to the language. If they had not seen anything, they would not have done it ... so why?

Also: https://stackoverflow.com>

EDIT2: Seeing that this question caused a lot of bad things that were sad about the goto statement, I went and asked my father. He is 52 years old and is an industrial engineer.
He told me a couple of times that he was doing good programming in his day and mostly in FORTRAN and COBOL. He is currently involved in IT services, server and network agency, etc.

In any case, he said something about "back in time ..."
After discussing this a bit, he returned to the statement of goto, saying that even at one time as a student, they all knew that it was not a smart idea to use it, but then they were not much better. It was still many years to try / catch, and error handling was practically not implemented.
So what did you do to test your program? Add a few lines at the end that allow you to print the output and all you need to check the code, and then you put the line: "goto printing" or something like that to start printing your data.

And so you gradually debug your code.

He agrees that using goto in the modern programming world is pretty useless. The only thing that he considers justified is the "emergency break", which will be used in extreme debugging and unforeseen situations. For example, goto fatal_error; and the "fatal_error" part of your code does some things to show you in-depth results. But only during the creation of something. The final product should not have operator operators.

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

+18
php goto
Apr 6 '09 at 21:31
source share
16 answers

If you are writing good PHP code, you do not need to use goto. I think this is the mistake they add, as it just leads to lazy programming.

Cm

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

For a good comment about adding this to PHP, and also here, when stack overflowing,

GOTO is still considered harmful?

+14
Apr 6 '09 at 21:37
source share

I just found two uses for goto :

  • To exit nested loops. But most newer languages ​​have a mechanism for this without goto anyway ( break <number> in PHP or break <loop label> in Java, etc.).
  • Go to the cleanup section at the end of the function. But then again, this is not often useful in language collected for garbage.

In other words, if you do not know whether to use goto for something, you should not.

+10
Apr 6 '09 at 21:38
source share

It can be used for debugging purposes, so you do not need to comment or reorganize blocks of code just to temporarily change your workflow.

+2
Apr 6 '09 at 23:46
source share

The main use that I see when using gotos in a language is the ability to port across languages. I wrote a parser generator in C that generated gotos parsers (because it was easier to use gotos than using more strict control structures), and now porting it to PHP doesn't hurt so much.

+2
May 31 '10 at 7:24 a.m.
source share

goto can help reduce code duplication for stack expansion, in the pseudocode below:

 do A if (error) goto out_a; do B if (error) goto out_b; do C if (error) goto out_c; goto out; out_c: undo C out_b: undo B: out_a: undo A out: return ret; 

(Robert Love's pseudo-code taken from the Linux kernel archive mailing list: https://lkml.org/lkml/2003/1/12/203 )

+2
Jul 16 2018-11-11T00:
source share

There is no such thing as good use of goto.

Maybe, maybe it can be useful to exit several nested loops, but you can already do this using "break 2", etc. Labeled breaks, like in Java, would be better than goto for this purpose.

Perhaps this is also useful for code written without using exceptions, when you need to skip to the end a bunch of statements if one of them fails. But this is just a fix for shitty code with crappy code.

+1
Apr 6 '09 at 21:33
source share

I admit that I never used goto in my codes. :)

The only reason for me, apparently, facilitates the shortest migration route from other languages ​​to PHP (almost only changing the language without touching the control structures) and code refactoring at the second stage of porting.

Personally, I believe in educated colleagues, and since they can avoid conditional breaks from loops, they can resist the temptation of goto.

+1
Apr 7 '09 at 10:53
source share

In classic VB coding, using goto is convenient for emulating try / catch error handling, for example:

 Function MyFunction() as String '-- start of error block ' On Error Goto Catch ' do something here that might cause an error MyFunction = "IT WORKED" Exit Function Catch: ' error occured - do something else MyFunction = Err.Description ' '-- end of error block End Function 

... and here is a way to emulate try / catch / finally ..

 Function MyFunction() as String '-- start of error block ' On Error Goto Catch ' do something here that might cause an error MyFunction = "IT WORKED" Goto Finally Catch: ' error occured - do something else MyFunction = Err.Description Err.Clear Finally: ' put your finally code here ' '-- end of error block End Function 

It can also be useful for cleaning at the end of a function, although, I suppose, you could make a case where you can call another function for this cleaning.

Honestly, I never had a case in PHP where I thought to myself: "Hmm, I'm sorry that there was no goto statement." I didn’t understand why they decided to do this, but these guys are pretty smart and have used PHP in very good directions so far, so maybe we expect that we are not yet aware.

+1
Apr 7 '09 at 13:53
source share

Goto is mainly used when writing finite state machines. When parsing contextual free grammar, you really need one of them. Although we could live without goto if continue $case; is a valid operator in the switch block to go to another case and without a course having register ranges, like many languages ​​at present. Until then, we pretty much got stuck with goto.

+1
Sep 28 '10 at 14:30
source share

Sometimes I use goto to avoid multiple nested ifs. This is not only about the logic, structure or flow of the program, sometimes it can only be about how the code looks.

+1
Aug 31 '17 at 13:11
source share

goto should really be something, although it was in the language and would be deprecated due to best programming practices. Adding it now looks like a step back.

0
Apr 6 '09 at 23:56
source share

As already mentioned, goto is really only required in some types of algorithms, usually those that arise when parsing a language or in machines with a final state. I have never missed the goto flaw in PHP.

OTOH, I programmed in a language where the only two structures were functions and conditional gotos: SNOBOL4 . Since the risk of spaghetti code was so high, most SNOBOL4 programmers were / tried to avoid this. But gotos did include very complex programming, creative loops, etc. Actually, it’s slightly easier to execute FSM loops if you have gotos.

0
Apr 7 '09 at 0:44
source share

The generated code can make good use of goto, I think. The good thing about the generated code is that you do not need to maintain it - you are just updating it.

0
Dec 14 '09 at 11:23
source share

The great advantage of gotos is the learning curve. I wonder why tools like visual studia and macs succeed. The reason is that people want more than a great product; they want a great product that they can learn to use in just an hour or so. Now many programmers for several days only one of their jobs. I see that many books say that you should never use goto, and then give five or so technologies that are said to eliminate every need for it. I say that it is the fact that they mentioned 5 is proof of how good they are !!!!! I don’t have time to learn five things that include exception structures, which take whole chapters to explain !!!!! when all you really need is a simple transition that can be explained in 30 seconds. Of course, you can create bad code with them if the programmer wants to --- but hey, most programmers do not want to write bad code, and if they could anyway. Most of the gotos in our lab have made the code very easy to understand and learn; much more than reading 2,000 pages of a book.

0
Jul 27 '13 at 4:34 on
source share

GOTO, a limited performance management framework, can be used instead of loops, but this is highly discouraged. Its use tends to encourage the creation of unstructured code, which is terrible practice. Most likely, this is best used only in development, for debugging (skipping large amounts of code to access a specific problem area) and testing. The only other goal for GOTO is perhaps to write assembler; most likely not. GOTO, if used outside of development, should be used sparingly and only as a last resort. If possible, replace GOTO with the applicable contour structure.

Regarding the last linked thread ( GOTO command in PHP? ):

As Ishmael says (edited by Gordon, the best answer is by far):

GOTO is simply an extended BREAK, with the ability to "use static tags". "Basically, this will improve the ability to exit nested if statements."

0
Nov 13 '14 at 18:20
source share

Short answer: goto is a workaround for limited stack space with much better performance in single-threaded code. Besides addressing stack space or performance, using it would be at least unnecessary and, at most, inappropriate, as it causes unnecessary complexity.

In over 2 million lines of code, I wrote in all languages, excluding machine code :-). Only two times its use was necessary, and both due to checking \ sorting large datasets of the tree.

0
Apr 21 '17 at 23:09 on
source share



All Articles