Why GOTO Fell Out of Favor: From BASIC to PHP Examples
The article recounts the author's early programming experience with GOTO in BASIC on an MSX computer, explains why modern languages discourage GOTO due to readability, debugging, and maintainability issues, and demonstrates a PHP CSV-reading example rewritten with goto labels to illustrate the resulting spaghetti code.
Reading font

At age 12, the author began programming on an MSX computer using BASIC, where the GOTO statement was a primary control flow mechanism. This early experience highlights how GOTO enabled loops and conditional jumps in an era without structured constructs.
Why Modern Languages Discourage GOTO
Readability: Heavy reliance on goto makes code difficult to understand, especially for developers unfamiliar with the codebase. Excessive goto statements lead to tangled execution flows.
Debugging: Tracing execution through frequent goto jumps is challenging, complicating error diagnosis.
Maintainability: Code with goto is harder to modify safely, increasing the risk of introducing bugs.
Structured Programming: goto violates structured programming principles, which advocate for loops, conditionals, and functions to create organized, comprehensible code.
Scope Issues: Jumping across scopes (e.g., into a loop or conditional block) can cause undefined behavior and is considered poor practice.
Although goto can occasionally exit deeply nested loops or implement specific control patterns, structured alternatives ( for, while, foreach, if/else) are preferred for clarity and maintainability.
Code Comparison: Structured vs. GOTO-Based CSV Parsing in PHP
The author takes a standard PHP example from the official documentation that reads and prints a CSV file using while and for loops:
<?php
$row = 1;
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "$num fields in line $row: " . PHP_EOL;
$row++;
for ($c = 0; $c < $num; $c++) {
echo " " . $data[$c] . PHP_EOL;
}
}
fclose($handle);
}This version is then rewritten using only if and goto with labeled statements:
<?php
start:
$row = 1;
if (($handle = fopen("118864.csv", "r")) === false) {
goto goodbye;
}
loop:
if (($data = fgetcsv($handle, separator: ",")) === false) {
goto goodbye;
}
$num = count($data);
echo "$num fields in line $row: " . PHP_EOL;
$row++;
$c = 0;
loopLine:
if ($c >= $num) {
goto endLoopLine;
}
echo " => " . $data[$c] . PHP_EOL;
$c++;
goto loopLine;
endLoopLine:
goto loop;
closeFile:
fclose($handle);
goodbye:
echo "Goodbye";Label Explanations
start: Marks the entry point of program execution. loop: Begins the outer loop that processes each CSV line. loopLine: Starts the inner loop iterating over fields in the current line. endLoopLine: Marks the end of the inner loop after all fields are processed. closeFile: Intended for closing the file handle (not actually used via goto in this example). goodbye: Terminates the program, printing "Goodbye" on error or normal completion.
These labels and goto statements create a non-linear control flow that is significantly harder to follow than the structured version. The article concludes by reiterating that excessive goto usage harms understandability and maintainability, and recommends structured constructs whenever possible.
References
PHP documentation example "Reading and printing the entire contents of a CSV file":
https://www.php.net/manual/en/function.fgetcsv.php#refsect1-function.fgetcsv-examplesPHP goto control structure:
https://www.php.net/manual/en/control-structures.goto.phpFor questions about attribution, corrections or content rights, please contactand we will review it promptly.