PHP Fatal Errors Part 2
Spend the last couple of days trying to figure out how to (finally) deal with PHP fatal errors. Right now the site displays the (almost) white screen of death but understandably The Client wants something a little more user friendly.
So I came across this technique of handling fatal errors. Basically you:
- auto-prepend a ‘fatal error detector script’ to every page
- if a fatal error occurs wrap the error message with bogus HTML tags (e.g.
<phpfatalerror>) - have the ‘fatal error detector script’ look for these bogus HTML tags
- if the tags are found… log the error message and overwrite the output (i.e. the white screen of death with the fatal error message) with something more meaningful
I wasn’t very comfortable with overwriting the output so I just redirect to my custom 500 page:
if (preg_match('|<phpfatalerror>.*</phpfatalerror>|s', $output,
&$matches))
{
foreach($matches as $match)
$errors .= trim(strip_tags($match));
$log->error($errors);
header('Location: ' . $_SERVER['HTTP_HOST'] . '/500');
exit();
}
Now although the above trickery works and admittedly is pretty clever… it still feels somewhat like a hack. Is it really that difficult for PHP to catch fatal errors with exceptions?
All I Wanted Was A Dashed Line
The Project has this shopping cart component which displays all the current items in your cart and nicely displays all the totals in a formatted manner:

See that dashed line? It doesn’t look so great across browsers. In particular in IE6/7 the dashed line comes up a little short:

I was hard-coding that dashed line in the HTML. It looked like this:
<td class="total">---------------------</td>
So I played around with the number of ‘-’ so that it would look half decent across all browsers. But I wasn’t satisfied because I knew my ’solution’ wasn’t the right one. So I talked to The Designer and he suggested that instead of putting dashes just set the top border to dashed:
<td class="total" style="border-top: dashed 1px #000000"></td>
So damn obvious. And it works like a charm…
Displaying Zebra Tables In Smarty
How come I’ve never heard of {cycle} before? It turns crap like this:
<table>
{section name = i loop = $items}
{if $smarty.section.i.index % 2 == 0}
{assign var = 'rowStyle' value = 'odd'}
{else}
{assign var = 'rowStyle' value = 'even'}
{/if}
<tr class = "{$rowStyle}">
...
</tr>
{/section}
</table>
Into concise stuff like this:
<table>
{section name = i loop = $items}
<tr class = "{cycle values = 'odd,even'}">
....
</tr>
{/section}
</table>
Which may not seem like much until you realize that The Designer uses zebra tables everywhere in The Project.
What The Hell Is An Array Map?
How to trim all values of an array without iterating and calling trim on each value:
$array = array_map('trim', $array)
Now although the above is quite handy, it’s these kind of little functions that makes PHP development frustrating. I mean how hard would it be to have an Array namespace in there? And what’s with the ‘passing the name of a function as a parameter of another function’?* I see that everywhere in the PHP docs. Coming from Java that is such a foreign concept to me.
* I’m sure there’s a name for this
Happy Birthday To Me
I turn another year older today.
To celebrate I implemented showing an animated Ajax updating gif while waiting for an Ajax request to finish:
var xmlHttp = <a new instance of an XHR> xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { // do your ajax stuff here } else { // load the updating gif here } } }
The Designer thought it looked pretty.
I would have preferred the day off but I’ll take what I can get.