Is GOTO in PHP evil?

I recently learned that PHP 5.3 supports a new language construct called GOTO . Everyone knows what he is doing. However, this is not quite a traditional GOTO , it is just a transition label. I am interested to know if this GOTO evil and implies bad code?

+64
php goto
Dec 14 '09 at 10:04
source share
12 answers

If you don't program in assembly language, GOTO should always be considered the same as a vest for the life of airplanes: it's good to have them available, but if you need to use them, it means that you have big problems.

+127
Dec 14 '09 at 10:12
source share

I can’t believe that no one posted this :)

xkcd - goto

Of course, PHP is not compiled ... Maybe a predator will chase you every time you visit your site?

+92
Dec 14 '09 at 10:30
source share

Poor structuring of evil code, regardless of the management structure used.

I personally prefer goto, which clears the program flow to "manage variables" and the nested "if", which indirectly leads to the same branch in the code.

So, just write two versions (with and without GOTO) and see which one is easier to understand. Then the choice is simple.

+67
Dec 14 '09 at 10:10
source share

I am outnumbered (currently), but I believe that the restrictions set by goto PHP make it very useful:

http://adamjonrichardson.com/2012/02/06/long-live-the-goto-statement/

I am actually looking at an example of arrow code (deeply nested conditional expressions) and reorganizing it using standard methods (security offers, grouping conditions, pulling functions) in one version and goto based version in another version, and I actually prefer refactoring based on goto.

+18
Feb 06 2018-12-12T00:
source share

I think this is the most important part of the PHP man page and is missing here:

This is not a complete unlimited transition . The target label must be in the same file and context, which means that you cannot jump out of a function or method and cannot go into one . You also cannot go into any loop or switch structure. You can jump out of them, and a common use is to use goto instead of a multi-level gap.

IMHO this is very different from the obsolete BASIC styles.

+16
May 28 '13 at 10:53
source share

Are guns evil? Both can be used for good or evil. I would say that it’s easier to write good code without goto than with.

+7
Dec 14 '09 at 10:06
source share

Any language feature that can make the code more readable in a given situation is a good thing. GOTO is one of such features of the language, even if such situations are few and far between. If we forbade any syntax that would allow poor programmers to write poor, uncontrollable code, our tasks would be much more complicated.

+6
Dec 14 '09 at 10:16
source share

As a programmer, I mainly work on "mainframes" and "large corporate servers" ... And our daily language (I mean one of 95% of our base code) is Cobol, which makes extensive use of GOTO.

This use does not mean that the code is bad. It just means that this tool (GOTO) was right the moment the programs were written.

To answer the question of Kaitsuli, I think that this can be a useful tool for writing PHP scripts. On the other hand, for almost a decade, many scenarios have been achieved without him. Furthermore, this is contrary to the evolution of PHP with more object-oriented functions.

IMHO, this is neither a good nor a bad thing for the code: good programs will still be good, and "horror programs" will be worse ... The only question is: "Why add GOTO 10 years after confirming that it was not necessary? "

+4
Dec 14 '09 at 10:48
source share

GOTO is usually evil because it allows you to create unstructured code. Using regular loops, you can easily build good structured code because it is structured.

When you have unstructured code jumping from here to there, you have just discovered the evil emanating from the GOTO instruction. It is almost always better to avoid this. Maybe every 100,000 lines there is a place where the GOTO clause simplifies the LOT, so the code is not evil, but if you are not sure, you should avoid GOTO.

Hope this helps.

EDIT: Well, just to add my own opinion here, there are other instructions that allow you to create unstructured code and that are not considered evil when I think they should be.

For example, returning in the middle of a function is GOTO to the end, so I avoid them and use only one return in each function only at the end.

Other languages, such as Vb.Net (possibly others), allow you to do Exit For, Exit While, breaks, etc., which also unstructure the code, and I think this should be avoided.

+3
Dec 14 '09 at 10:10
source share

sometimes (I mean in 0.01% of cases), this is useful, for example, when you have a long, long script and you want to test some blocks. but never leave it in your last script

+2
Aug 26 '13 at 16:16
source share

I used GOTO when I write a script to work in cli mode. It saves my life.

+1
Jan 04 '13 at 11:52
source share

GOTO should be removed from the language. If someone needs to “unconditionally transfer control,” then they need to go back to school to learn Structured Programming 101

-eleven
Aug 23 '12 at 15:08
source share



All Articles