+ 1

Why goto function is bad practice in php ?

I find some advices in google that goto function is a bad practice. when i tried to test it with replace for loop with goto function i notice that with goto function take less time than for loop. Do you think is realy bad practice ? https://code.sololearn.com/wzCgWyVsBtR2/?ref=app

9th Mar 2019, 1:06 AM
Elghozi Nasreddine
Elghozi Nasreddine - avatar
2 Answers
+ 3
Not only in php, but in many other languages. The usage of goto creates a code not well structured. It introduces non-block flow control which can be hard to maintain and read. I'm not saying it needs to be avoided, but it needs to be used wisely.
9th Mar 2019, 1:16 AM
Mauricio Martins
Mauricio Martins - avatar
+ 3
The problem is it's hard to track when your program becomes longer. If you use goto as your main structural logic, you can reach every place from everywhere in your code, every variable is changable from anywhere... imagine a program with even only 10000 lines and something goes wrong. Where's the mistake? By using functions that have their own scope and can't interfere with other places, it becomes easier to track what's happening, and building on that idea classes, modules, packages or whatever sort of encapsulation structure helps you reducing the potential scope of disaster. Goto is like an artifact of old times. Like what people did before they knew better.
9th Mar 2019, 1:21 AM
HonFu
HonFu - avatar