15 ms·
PHP introduces "goto"
- noodle 17y agough. they caved. too bad. this kind of hurts their movement towards OO. now we'll probably see see tons of people using goto as bandaid measures instead of learning better techniques to solve their problems. look out for raptors.
- dryicerx 17y agoIt's PHP, it's already a band aid measure. Adding goto just incremented the keyword count, nothing more. Bad coders will still keep on writing bad code, with or without the help of goto.
- jcapote 17y agoI wish I had more points to give this comment
- ars 17y agoWhy? PHP is not any worse or better than any other language. You can have bad programmers in any language - PHP just has lots of them because it's a popular language, but there is nothing wrong with the language itself.
- SwellJoe 17y agoI would argue that effectively having roughly 3,000 keywords is a problem of the language itself. And, saying, "PHP is not any worse or better than any other language" is saying that all languages are equal. Which, having worked in a number of languages over the years, including PHP, I know to be false. PHP is worse than many languages, and better than many for some classes of problem. It's just silly to say that all languages are equal.
- ars 17y agoPHP does not have 3000 keywords. It has an ordinary number, and the rest are functions - basically the standard library. You are confusing the language and the library. And for that matter the library is not bad either, it does however have a few poorly named legacy functions. > It's just silly to say that all languages are equal. Sorry, I wasn't really trying to say that. I meant "loosely typed procedural languages" not "any other language". You pick the language the matches what you are doing, there is nothing inherent about PHP that makes it bad.
- SwellJoe 17y agoPHP does not have 3000 keywords. It has an ordinary number, and the rest are functions - basically the standard library. A library function that is not optional is effectively a keyword. The PHP standard library (all ~3000 functions) is not optional, as far as I know.
- nir 17y agoMany (most?) of them are optional, you can compile PHP with/without certain extensions. Design wise, it seems insane - kind of like the iPhone's non-removable battery. Turns out it doesn't hurt either one's popularity though...
- SwellJoe 17y agoOK, so I really didn't think I needed to specify "at run-time" with regard to the meaning of "optional".
- ars 17y agoThey can be done at runtime, you can dl() whatever modules you want. However no one bothers. Why? Because while having lots of keywords is a problem in theory, in the real world it doesn't matter at all.
- noodle 17y ago
- jon_dahl 17y agoI take it your test of language quality is Turing completeness?
- ars 17y agoAnd what would you like to change in PHP? Right now - what would you change. Keep in mind it's a loosely typed procedural language - that's not going to change. Given that, what would you change?
- jey 17y ago"Why? PHP is not any worse or better than any other language." False. Not all languages are created equal, and PHP is on the shittier end of the spectrum.
- ars 17y agoReally? So back up the statement - tell me exactly what's bad about PHP. And as I said before, it's a loosely typed procedural language, so don't try to compare it to a functional one. Within that type of language, what's bad about PHP?
- encoderer 17y agoI like PHP just fine and use it often. But if you really want an answer to that question, I'll give you one: inconsistency. Not saying I don't understand it, and I'm not saying I blame the way PHP has evolved. But it does have a lot of inconsistency.
- holygoat 17y agoAre you aware that your challenge is rapidly approaching "without reference to anything better, tell me about PHP's failings"? The usual technique for evaluating something is to compare it to its competitors, and some of those are functional! One of the bad things about PHP is that it lacks the higher-order capabilities of most functional languages. It's also inconsistent, easy to write slow code in, makes it easy to introduce massive vulnerabilities, and ugly. It's 2009. Nobody should be using PHP any more.
- ars 17y agoPHP is not a functional language. People like functional languages in theory, for the style. But not so much when it comes to actually using them. And what's ugly about PHP? It looks almost exactly like C. It is inconsistent (sometimes), but it's not "easy to write slow code in", there is nothing in PHP that makes slow code more or less likely. It's 2009. Everyone IS using PHP, and almost no one uses functional languages. Oh they get a lot of press, but few deployments. I'm really tired of these illogical attacks on PHP. No one has ever told me a problem in PHP compared to other procedural languages except that 0 == "" is confusing. It's always "but everyone knows it's bad". Sure go along with peer pressure if you like, but I prefer to look at reality.
- jrockway 17y agoPHP is missing many features that good programmers need to write good code. Anonymous functions, lexical environments, the list goes on. You can write OK code with PHP, but you can't write excellent code. (And no, I am not implying that code is bad unless you use a lot of anonymous functions. I am just saying that it's an essential tool that you really need when certain situations arise. In PHP, those situations will have to involve hacks.)
- ars 17y agoPHP has anonymous functions, so I guess now you love PHP. You say the "list goes on" - so I googled for a list http://www.tnx.nl/php.html http://www.tnx.nl/php.html. And half the things on the list aren't even true! And the rest are style choices (more functions with less parameters, or fewer functions with more parameters).
- jrockway 17y agoHere is a list: continuations, closures (which is what I really meant by anonymous functions and lexical environments, but I guess the term "environment" is not familiar to people that don't program CL), macros, real OO (classes/traits, first-class attributes, a metaobject protocol, etc), a sane type (constraint) system, and so on. Nobody cares about the order of arguments to a function in the standard library, that's trivial to ignore and fix. What PHP is missing is a way to extend the language, which means you are stuck with whatever the designers give you (which sucks hard). Anyway, if you haven't used these features before, you might not see the need; see the "Blub" essay for details on that.
- weavejester 17y ago> PHP is not any worse or better than any other language. I disagree. Consider the following PHP: function inc_all($i, $xs) { array_map($xs, function($x) use ($i) { return $x + $i; }); } And compare it to the equivalent in, for instance, Haskell: inc_all i = map (+ i) Most languages can express the same concepts as PHP, but can do so much more clearly and concisely. The criticism of PHP is deserved: it's a very badly designed language.
- ars 17y agoHaskell is a functional language, PHP is not. So it's not a valid comparison. Every time I ask people what's wrong with PHP they give me answers from functional languages. I've yet to see anyone give me an example from a procedural language.
- psygnisfive 17y agoYou're right. Let's compare to a more adequate language. Like Ruby. def inc_all(i, xs) xs.map { |x| x + i } end
- jamesbritt 17y ago"PHP is not any worse or better than any other language." Haskell fits as "any other". "Haskell is a functional language, PHP is not. So it's not a valid comparison." So, PHP is not any worse or better than any other language that's just like PHP?
- lhorie 17y ago> PHP is not any worse or better than any other language. I also disagree, but in the sense that PHP is both better and worse than other languages, depending on your metrics. I feel that talking about code terseness is weird if it takes more effort to actually complete whatever project you're working on (due to lack of libraries, less active community, what have you).
- joshu 17y agoI'm pretty sure it's actually worse.
- huhtenberg 17y agoOh, common. Are we talking about the language that allows run-time renaming of the variables ? I mean if you have that then adding a goto hardly qualifies as "too bad".
- wvenable 17y agoHow exactly do you rename a variable?
- jrockway 17y agoI suppose he means something like: $foo = 42; $bar = 69; $which_one = 'foo'; print eval "\$$which_one";
- deleted 17y ago[deleted]
- encoderer 17y agoActually, you don't need the eval in there. print $$which_one will do just fine
- huhtenberg 17y agoActually, no. That's not it. This is an equivalent of a pointer to a variable in C. What I was referring to is the actual renaming the variables in run-time, i.e. change variables' key in the table of variables. So the code like this: for ($i = 0; $i < 8; $i++) { .. <rename i to j> .. } would throw a run-time error on the second iteration, because $i will become undefined. It was completely and utterly "out there", that's why I remembered it. Also showed it to a bunch of people, all of them were equally amazed. But now I can't seem to find where I saw it and what the exact syntax was.
- randallsquared 17y agoI don't know how you'd do that in PHP, but I'm pretty sure that you can do it fairly easily in Python and Lua, and I've never heard anyone claim that (more or less) first class environments are a feature of bad languages.
- jrockway 17y agoFWIW, Perl has had goto forever, and I have never encountered it in real code. It just isn't necessary. So I doubt this will make PHP any worse than it already is. They should really consider adding some useful features, though.
- phaylon 17y agoAnd even if it's seen in Perl land, in most cases it is called with a code reference as argument, not a label, which simply replaces the current item on the calling stack with the code reference. This allows for manual tail-call-optimisation which is quite nice sometimes.
- deleted 17y ago[deleted]
- jnorthrop 17y agoThis feels like a giant step towards the past.
- ComputerGuru 17y agoThis is a giant step towards the past. Welcome to 1980.
- deleted 17y ago[deleted]
- encoderer 17y agowhile() loops are just goto's with some syntactic sugar..
- jcsalterego 17y agoWhile loops are restricted gotos with syntactic sugar.
- ComputerGuru 17y agoNow that I come to think of it, it actually doesn't matter for PHP. While loops limit the scope of variables inside the loop. For C++, that means a variable declared inside cannot be used outside - logical, and OOP. But PHP has no concept of privacy or scope, even its "rules" may be broken (though not guaranteed to always work) and come out with the right results. Anything will compile, and usually the results are as expected. So if you have a while loop with no concept of scope or local variables, I guess the restrictions are gone. A number of nested loops with breaks; and continues; all over end so long as the concept of limited data scope/access is not present as in PHP are exactly equivalent to goto as it is. I guess a language as, shall we say, 'relaxed' as PHP is well-matched with relaxed keywords like goto.
- pbiggar 17y agoNo, thats not what he means. He means that gotos are unstructured, and you can do anything with them, like jumping into a loop. Although, PHP's gotos don't allow they, and PHP still only admits structured control flow.
- travisjeffery 17y agoHaha, wow how sad. The kind of sadness created when realizing how disgustingly pathetic our evolution can often be. I can't wait to spend minutes of my life when I have to go through another programmer's PHP code who uses gotos.
- encoderer 17y agoI think you left out your goto CalmDownFirst line in your DramaQueenPreventor class.
- jdp 17y agoWhy is everyone jumping on this as a negative? Goto isn't inherently evil. Of course in most cases there is a construct that would be a better fit than goto, but it does have its uses. As far as readability goes, odds are a programmer isn't going to be jumping to a label 300 lines up, its used mostly to break out of nested loops and to simulate stuff like state machines. Check out http://david.tribble.com/text/goto.html http://david.tribble.com/text/goto.html
- pygy 17y agoThey already have a break statement to get out of loops of arbitrary depth. http://www.php.net/manual/en/control-structures.break.php http://www.php.net/manual/en/control-structures.break.php And why, except perhaps for the hack value, would you simulate a state machine in PHP? GOTOs are good when used wisely in certain contexts. I don't see where they could be of any use in the PHP niche (but I'd be glad to hear about such examples). Seasoned PHP coders don't need it. Besides, PHP is mostly a beginner's programming language, who learn by example using code found online. They will be exposed to even worse practices from day one.
- deleted 17y ago[deleted]
- wooby 17y agoIt's not negative in that, people can program using whatever language they want using whatever language constructs they want. But for PHP, which is mostly for web development, I would think using exceptions is a much peer-friendlier way of handling what you'd probably be using goto for in a web app. If you're writing state machines or parsers, PHP probably isn't the way to go anyway. But hey - whatever floats your boat.
- Scriptor 17y agoIt takes a great deal of discipline to not use goto for quick fixes when you feel lazy. Unfortunately, many PHP programmers simply don't have the experience to properly structure code, since PHP is their first actual experience with programming. While major projects like frameworks and well-established CMS's will likely do fine, smaller-scale projects and codebases will suffer.
- raffi 17y agoI've thought of adding goto to Sleep. Have you ever looked at the code in the Linux kernel? It's full of gotos. They use goto's to make sure functions have one exit point and to cleanly deal with errors in one spot.
- paulgb 17y agoKernel hackers know presumably what they're doing and use goto only when they have a good reason. Can the same be said of PHP programmers? I'm all for the "don't protect programmers from themselves" philosophy, until I'm the guy who has to use or maintain the code.
- look_lookatme 17y agoFrom the page: "It is not allowed to jump into a loop or switch statement. A fatal error is issued in such cases." At least that isn't possible. What a mess it would be if this wasn't the case.
- abyssknight 17y agoI feel like I've been trolled by the PHP haters again. All of this language-ism is killing my chi.
- invisible 17y agoI agree wholeheartedly. C++ has goto, and although it's clear C++ can't learn from the past, at least programmers for PHP can be warned to use goto conservatively. Error handling and portability of code from other languages will make PHP more widely used. That is the goal of the founders/contributors: make PHP used as widely as possible on it's features alone.
- abyssknight 17y agoI feel this is a relevant cross post, at least for my own amusement: http://news.ycombinator.com/item?id=650328 http://news.ycombinator.com/item?id=650328
- robotron 17y agoThis isn't some kind of surprise. It has been discussed for some time now. There is also no reason for a developer to use it if it's not wanted.
- deleted 17y ago[deleted]
- ars 17y agoYay! Finally I've been waiting for this for a long time. I can finally get rid of some spaghetti code: goto emulation via while loops. Yes, it's not often you need goto, but sometimes you do.
- pbiggar 17y agoNo way! I do exactly this. Switch statement, while loop, and lots of continue statements. Its a lowering of basic blocks that I want to print out to test. What's yours?
- sev 17y agoI understand why C and C++ have goto statements (as well as other older languages). But to actually "introduce" goto's in a language that didn't have it or need it, making it even possible to write such horrible code is, in my opinion, pointless and worthless. It promotes illegible and bad code in general.
- tamersalama 17y agoAnyone knows where's the PHP core team blog/site (if there's such team)?
- tamersalama 17y agoSomeone pointed out the minutes-of-meeting http://www.php.net/~derick/meeting-notes.html#adding-goto http://www.php.net/~derick/meeting-notes.html#adding-goto
- sil3ntmac 17y agoSweet! Hope they implement COMEFROM next. http://en.wikipedia.org/wiki/COMEFROM http://en.wikipedia.org/wiki/COMEFROM
- mynameishere 17y agoMost langauges have that. It's called "catch".
- anigbrowl 17y agoIt's a bit late for that. http://cadie.googlecode.com/svn/trunk/ http://cadie.googlecode.com/svn/trunk/
- e4m 17y agoI once used that a lot on my C64. Made sense back in 1982 and in limited cases, makes sense today.
- deleted 17y ago[deleted]
- dflock 17y agoThe first user contributed note on that page: <?php wtf: echo 'For real!?'; echo 'Srsly!?'; goto wtf; ?> Heh.
- behe101 17y agoMy first thought when I saw this: "Djikstra is spinning in his grave."
- nir 17y agoSo what? If you think goto is harmful, don't use it. If you think PHP in general is harmful, don't use that either. Maybe help build something comparable to Wordpress or Drupal in your favorite language so other people can use it instead of PHP. If all you have to offer is empty sarcasm, goto Reddit.
- tetha 17y agoAnd then you have to maintain spaghetti code in PHP with gotos.
- apgwoz 17y agoFinally! Now PHP really is the web's assembly language.
- antirez 17y agoGoto is a good idea in C, not only for the obvious low-levelness reasons, but also because if you can write C you are almost certainly a bit wiser than the average PHP programmer that may use goto for things I prefer don't think about before to go to sleep :)
- uriel 17y ago"If you want to go somewhere, goto is the best way to get there" -- Ken Thompson
- ahlatimer 17y agoI was working for a company doing mostly COBOL programming about a year ago, and I can honestly say that GOTO can be used neatly and effectively, but it can also turn code into a horrible mess. For COBOL, it's an absolute must. There are no while or for loops in COBOL (at least not in the version we were using), so you had to replicate that with GOTO's and paragraphs. For PHP, I really don't see this as necessary. So long as it's not widely publicized in tutorials and the like, I don't really see a whole lot of novice coders getting their hands on this. I'm not particularly concerned about experienced programmers using GOTO in PHP, especially if they've used in another language where it is more necessary.
- patryn20 17y agoSo....all the progress PHP has made in recent years has not been undone.....but has been rendered irrelevant. Never again will anyone be taken seriously while using PHP. It will once again be purely the domain of amateurs and script kiddies. This is being said as someone who has created marvelous applications in PHP and has dealt with all the PHP language shortcomings up to this point. Makes me truly, truly sad.
- drawkbox 17y agoThis helps quite a bit... It helps people move on from PHP because it is the 'language that later added goto'. A sad day for programmers in PHP. gotos are a bad programmer's recursive method. Even people that use while loops like pepper scare me a bit. I have seen many'a'app get stuck in memory draining death loops with goto and even while(). Flags are dead, long live events and messages.