A reader named Charles sent The Daily WTF a PHP block of more than 400 lines, anonymized to protect the guilty. Charles is the only IT person at his company and needed somewhere to vent. Editor Remy Porter obliged.
<?php
$resm_data = $data_source->fetchData("group=" . $item_id);
foreach ($resm_data as $key => $value) {
$option_id = $value->option_id;
$resm_details = $detail_source->fetch($option_id);
if ($resm_details) {
$label = $resm_details->{"label$lang"};
$category = $resm_details->category;
if ($category == 0) {
$cost = $resm_details->{"cost" . $currency};
$cost_info = $mot[101] . " : +" . number_format($cost, 2, ",", " ") . $currency_symbol;
}
}
}The code builds a booking form. It starts with $data_source->fetchData("group=" . $item_id), a filter condition glued together as a raw string, which suggests SQL injection depending on the library underneath. Localization works through dynamic property names: $resm_details->{"label$lang"} yields labelen or labelde, and prices come from {"cost" . $currency}, with occasional _1 and _2 suffixes for extra tariffs.
A six-branch if/elseif chain on a category field then formats prices with number_format($cost, 2, ",", " ") and pulls UI labels from a mysterious $mot array at magic indexes like 101, 102, 200, 300 and 500. HTML is assembled by string concatenation, including a quantity input hardcoded to min="1" max="1". Departure and arrival times are stored as JSON strings in the database and decoded per row.
Everything lands in a template via $TEMPLATE->SET_BLOCK_VARIABLES("Block_OPTIONS", [...]) with 18 keys. Then the entire block appears again for child records, every variable prefixed with child_. Then a third time for siblings, fetched with fetchData("parent=" . $parent_option_id) and rendered into the same "Block_OPTIONS.Block_OPTIONS_CHILD" block.
Porter closes with a wish: that the author one day discovers functions.
Comments
No comments yet — be the first.
Open the discussion
No account or password needed — just enter your e-mail and we’ll send you a one-time sign-in link. First time here? You’re set up automatically.
Your rating will be applied automatically after you sign in.
Check your inbox
We’ve sent a sign-in link to …. Open it on this device — this tab will sign you in automatically.
Nothing arrived? Check your spam folder — and mark the mail as "Not spam" so it lands in your inbox next time.