The first instinct for retry logic is usually while (true). It works, but the try/catch ends up nested inside the loop, adding an extra indent level and burying the intent slightly.
PHP has goto, and most people skip right past it. But for retry logic specifically, it’s one level flatter and the intent is clearer.
The while Version
The common approach:
| |
It works fine. But a few things are slightly awkward:
while (true)exists purely to “jump back” β it carries no business meaning- The entire try/catch is pushed one level right
- The success path exits via
returnfrom inside a loop β it’s a side exit, not a natural one
The goto Version
| |
The flow is direct:
- Success β
return - Failure with retries remaining β
goto beginning - Failure with no retries left β
throw
The jump only happens when explicitly retrying. There’s no implicit “loop continues” logic to reason about.
The Structural Difference
| |
That saved level becomes very noticeable when the try block is long.
PHP goto Constraints
PHP’s goto has a few rules:
| |
As long as the target label is in the same function and not inside a loop or switch body, it’s valid.
Why goto Has a Bad Reputation
Historical reasons. In the C era, goto was heavily abused to produce spaghetti code with jumps flying everywhere. Dijkstra’s 1968 letter “Go To Statement Considered Harmful” cemented goto’s association with bad code, and the reputation stuck.
But that critique was aimed at arbitrary jumping, not every use of goto. Jumping backward for retry logic β with a clear, local target and obvious intent β is nothing like the kind of goto that caused problems.
In practice, the Linux kernel still uses goto for cleanup paths in C. You can find occasional goto in Symfony and other major PHP codebases too.
When This Pattern Fits
The goto retry pattern works well when:
- There’s a fixed retry count
- Only specific exceptions trigger a retry (here:
ConnectException) - There’s a delay between retries (
usleep) - The logic is simple and the jump target is obvious
If the retry logic becomes more complex β exponential backoff, multiple exception types, logging β wrap it in a helper function instead of stretching goto further.
Summary
goto isn’t untouchable β it just needs the right context. For retry logic with a clear backward jump, no cross-function leaps, and an obvious target, goto expresses the intent more directly than while (true) and saves a level of nesting.
When you see goto, don’t flinch. Look at where it jumps and why, then decide if it needs changing.
References
- PHP Manual: goto β goto syntax rules and constraints
- Dijkstra 1968: Go To Statement Considered Harmful β The seminal paper critiquing arbitrary goto usage
- PHP Manual: Exceptions β Complete guide to try/catch and exception handling
