Dreaming of GOTO in PHP

Well, not actually dreaming. I was listening to Satie’s Gymnopédies and courting sleep when some PHP code started scrolling through the imaginary terminal window emblazoned on my mind’s eyelid. It was this method of handling errors in PHP:

do {
    code
    if ( error condition )
        break;
    code
    if ( error condition )
        break;
    code
} while ( 0 );
cleanup code

It seemed like a nice alternative to so many if trees and if/elseif/else hedges. It seemed so clean and obvious. It seemed too simple to have waited until 2007 to be imagined.

So I opened Firefox and typed “php goto” in the address bar (my new favorite way of searching recklessly) which led me to goto in PHP which linked to [PHP-DEV] GOTO operator (a 2004 discussion) and eventually a message from Andrey Hristov containing the very same idea. The thread goes on and on. Andi fights it hard, Rasmus drops in with a favorable opinion, and then everyone ignores him until I get tired of reading and go back to bed.

Epilogue: While reading, I recalled having seen what was basically the same idea in the PHP manual page for do-while loops.

Published by

Andy Skelton

Code Wrangler @ Automattic youtube.com/AndySkelton

5 thoughts on “Dreaming of GOTO in PHP”

  1. I just found goto in the php manual tonight, and wanted to see what other people are saying about it. It think it is a great new operator, and think it has the potential to clean up some messy code.

  2. Yeah goto definitely have it’s place, it’s funny though how some simple stuff manage to stay hidden for so long. It’s to develop stuff like this ourself, before it becomes popular, it shows how someone understand and not only use a language. Great food for though to see how we can improve stuff, one little little step at a time!

Comments are closed.