Is GOTO in PHP evil?

I recently found out that PHP 5.3 supports new language construct called GOTO. Everybody knows what it does. However, it's not exactly the traditional GOTO, it's just a jump label. I'm interesting in knowing whether this GOTO is evil and implies bad code?

47148 次浏览

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

Bad structuring of code is evil, regardless the control structure you use.

I personally prefer a goto that makes clear the flow of the program to "control variables" and nested "if" that will indirectly just cause the same branch in the code.

So, just write the two versions (with and without GOTO) and see which one it's easier to comprehend. Then the choice is easy.

GOTO usually is evil because it lets you build unstructured code. With the usual loops you can build good structured code easy to follow because it is structured.

When you have non structured code jumping from here to there, you have just found the evil coming from the GOTO statement. Almost always is better to avoid it. Maybe once every 100.000 lines there is a place where a GOTO sentence simplifies A LOT the code thus is not evil but if you are unsure, then you should avoid the 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 a return in middle of a function is a GOTO to the end of it so I avoid them and use only one return in each function just at its end.

Other languages like Vb.Net (maybe others too) allow to do Exit For, Exit While, breaks and things like these that also unstructure the code and I think should be avoid.

Unless you are programming in assembler, GOTO should always be treated the same way as the life vest of the airplanes: it is good to have them available, but if you need to use them it means that you are in big trouble.

Any language feature that can make code more readable in a given situation is A Good Thing. GOTO is one such language feature, even if those situations are few and far between. If we forbade any syntax that made it possible for poor programmers to write bad, unmaintainable code our jobs would be an awful lot harder.

I can't believe nobody posted this :)

xkcd - goto

Granted, PHP is not compiled... Maybe the raptor will chase you on every visit to your website?

As a software engineer, i mostly work on "mainframes" and "big corporate servers"... And our daily language (I mean the one in 95% of our base code) is Cobol, which uses extensively GOTOs.

This usage doesn't mean the code is bad. It just means that this tool (GOTO) was the right one at the moment programs were written.

To answer Kaitsuli's question, I think it can be useful tool when writing PHP scripts. On the other hand, a lot of scripts were achieved without it for almost a decade by now. Furthermore, it goes against PHP's evolution with more object-oriented features.

IMHO, it's nor good nor a bad thing for the code be produced : good programs will still be good and "horror programs" will be worse... The only question is : "Why adding GOTOs 10 years after proving it was not necessary ?".

I'm in the minority (currently), but I believe the restrictions placed on PHP's goto construct make a very beneficial tool:

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

I actually walk through an example of arrow code (deeply nested conditionals) and refactor it using standard practices (guard clauses, grouping conditions, pulling out functions) in one version and a goto-based version in the other version, and I actually prefer the goto-based refactoring.

I used GOTO when I write script for working under cli mode. It save my life.

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

This is not a full unrestricted goto. The target label must be within the same file and context, meaning that you cannot jump out of a function or method, nor can you jump into one. You also cannot jump into any sort of loop or switch structure. You may jump out of these, and a common use is to use a goto in place of a multi-level break.

IMHO this makes it very different from the ye olde BASIC style gotos.

sometimes (I mean in 0.01% of cases) it is useful, like when you have a long long script and you want to test some blocks. but never keep it in your final script