3v4l.org

run code in 500+ PHP versions simultaneously
<?php function showCell($cell, $isHeader, $selectedTitle) { echo '<td>'; if ($selectedTitle || $isHeader) { echo htmlspecialchars($cell); } else { echo sprintf('<a href="?title=%1$s">%1$s</a>', htmlspecialchars($cell)); } echo '</td>', PHP_EOL; } function showRow($cellsToShow, $isHeader, $selectedTitle) { if ($isHeader) { echo '<thead>', PHP_EOL; } echo '<tr>', PHP_EOL; foreach ($cellsToShow as $cell) { showCell($cell, $isHeader, $selectedTitle); } echo '</tr>', PHP_EOL; if ($isHeader) { echo '</thead>', PHP_EOL; } } function showTable($handle, $selectedTitle) { $idx = 0; echo '<table>', PHP_EOL; while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) { // If we're on the first row which holds titles, always show. // Also, if the user selected a title, and it matches this row's title, show it. if(0 !== $idx && $selectedTitle && $row[0] !== $selectedTitle){ continue; } // cellsToShow is always an array. If we're showing based on a selection, it is everything. // Otherwise, it will be an array of just the first cell. This last seems weird, but it // allows the showRow method to not concern itself with higher level logic. $cellsToShow = $selectedTitle ? $row : [$row[0]]; showRow($cellsToShow, $idx++ === 0, $selectedTitle); } echo '</table>', PHP_EOL; } // Mock a CSV file in memory for demo purposes $handle = fopen('php://memory', 'rb+'); fwrite($handle, "Title,Author,Year\nBook 1,Alice,2020\nBook 2,Bob,2019\nBook 3,Charlie,1980"); rewind($handle); // Grab the title from the query string, default to null if not found $selectedTitle = $_GET['title'] ?? null; showTable($handle, $selectedTitle); fclose($handle);
Output for 8.4.1 - 8.4.21, 8.5.0 - 8.5.6
<table> Deprecated: fgetcsv(): the $escape parameter must be provided as its default value will change in /in/33ose on line 35 <thead> <tr> <td>Title</td> </tr> </thead> Deprecated: fgetcsv(): the $escape parameter must be provided as its default value will change in /in/33ose on line 35 <tr> <td><a href="?title=Book 1">Book 1</a></td> </tr> Deprecated: fgetcsv(): the $escape parameter must be provided as its default value will change in /in/33ose on line 35 <tr> <td><a href="?title=Book 2">Book 2</a></td> </tr> Deprecated: fgetcsv(): the $escape parameter must be provided as its default value will change in /in/33ose on line 35 <tr> <td><a href="?title=Book 3">Book 3</a></td> </tr> Deprecated: fgetcsv(): the $escape parameter must be provided as its default value will change in /in/33ose on line 35 </table>
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30
<table> <thead> <tr> <td>Title</td> </tr> </thead> <tr> <td><a href="?title=Book 1">Book 1</a></td> </tr> <tr> <td><a href="?title=Book 2">Book 2</a></td> </tr> <tr> <td><a href="?title=Book 3">Book 3</a></td> </tr> </table>

preferences:
84.41 ms | 1159 KiB | 4 Q