Skip navigation.
Home

Why is my code so slow?

Hall of Fame | Bad Architecture

Don't you hate it when the Internet makes your PHP run slow? I do. Props to Andrew Lindeman for sending this snippet in. I trimmed it to reduce the boring.

This code checks the status of a bunch of Diablo II game servers and generates a pretty table that shows a server's status. Unfortunately the code runs really, really slow. Damn the Internet!

<style type="text/css">
<!--
...
-->
</style>
<p align="center" class="style1">Overall PKA Server Status</p>
<table width="100%" border="0" align="center" cellspacing="0">
 <tr bordercolor="#990000" bgcolor="#990000">
   <td><strong>Diablo II Realms </strong></td>
   <td bgcolor="#FF0000"><span class="style5">Status</span></td>
   <td bgcolor="#660000"><strong>Diablo II Game Servers 1 </strong></td>
   <td bgcolor="#660000"> </td>
   <td bgcolor="#660000"><span class="style6">Diablo II Game Servers 2 </span></td>
   <td bgcolor="#660000"> </td>
 </tr>
 <tr>
   <td width="172">Diablo II  Open Server is :</td>
   <td width="121"><?php
if ($fp = @fsockopen("bnet.pkaclan.com","6112")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
...
 </tr>
 <tr>
   <td>PKA Realm is :</td>
   <td><?php
if ($fp = @fsockopen("bnet.pkaclan.com","6113")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
   <td>D2GS PKA1.1(Soulzek):</td>
   <td><?php
if ($fp = @fsockopen("68.56.114.98","4000")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
   <td> </td>
   <td> </td>
 </tr>
 <tr>
   <td>PvP Realm is :</td>
   <td><?php
if ($fp = @fsockopen("bnet.pkaclan.com","7113")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
   <td>D2GS PvP1.09(<SPAN class=postbody>Shiznoo</SPAN>):</td>
   <td><?php
if ($fp = @fsockopen("141.158.150.28","4000")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
   <td> </td>
   <td> </td>
 </tr>
 <tr>
   <td>Ancestrall Recall Realm is :</td>
   <td><?php
if ($fp = @fsockopen("bnet.pkaclan.com","8113")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
   <td>D2GS AR1.10(Wallbot):</td>
   <td><?php
if ($fp = @fsockopen("wow.pkaclan.com","4000")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
   <td> </td>
   <td> </td>
 </tr>
 <tr>
   <td>Hell Unleashed Realm is : </td>
   <td><?php
if ($fp = @fsockopen("bnet.pkaclan.com","9113")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
   <td>D2GS HU1.10 (Gnecromancer): </td>
   <td><?php
if ($fp = @fsockopen("24.71.57.148","4000")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
   <td>D2GS HU1.10 (D2Classic): </td>
   <td><?php
if ($fp = @fsockopen("69.133.108.231","4000")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
 </tr>
 <tr bgcolor="#990000">
... more of the same ...

Generating nice HTML with PHP... forget the PHP.

Bad Architecture

Jumping in and out of HTML in PHP is a fundamental part of the language but what if you didn't know that? What if you wanted PHP to generate some nicely formatted HTML? Props to Thiemo M├Ąttig for sending in this WTF.

This is probably the most 'clever' way of adding in tabs and new lines to format HTML.

<?php
$n
= chr(13);
$t = chr(9);
$nt = $n.$t;
$ntt = $n.$t.$t;
$nttt = $n.$t.$t.$t;
$ntttt = $n.$t.$t.$t.$t;
$nttttt = $n.$t.$t.$t.$t.$t;
?>

and a little furthur...

<?php
$html
.= $nttt.'<table>';
$html .= $ntttt.'<tr>';
$html .= $nttttt.'<th>...</th>';
$html .= $nttttt.'<th>...</th>';
$html .= $ntttt.'</tr>';
$html .= $ntttt.'<tr>';
$html .= $nttttt.'<td>...</td>';
$html .= $nttttt.'<td>...</td>';
$html .= $ntttt.'</tr>';
$html .= $nttt.'</table>';
?>