Practical Web Programming

Wednesday, November 12, 2008

How to Make a Cyber-Child

A little boy goes to his father and asks 'Daddy, how was I born?'

The father answers, 'Well, son, I guess one day you will need to find out anyway! Your Mom and I first got together in a chat room on Yahoo. Then I set up a date via e-mail with your Mom and we met at a cyber-cafe. We sneaked into a secluded room, where your mother agreed to a download from my hard drive. As soon as I was ready to upload, we discovered that neither one of us had used a firewall, and since it was too late to hit the delete button, nine months later a little Pop-Up appeared that said:

'You got Male!'

Friday, November 07, 2008

Lessons from the PHP Mail Function

Oh man, I got screwed.

Yesterday, I was testing the mail function that I wrote for the Helpdesk web application that I did. I was so sure the function works because I always receive email every time I submit a test ticket entry.

I wrote the function to send an email only to me when it is the development server and send to everybody else in our team when it's in the production. The human (or idiot :)) that I am, I miss to change the $to parameter of the mail function. So instead of only me receiving the email, our entire team for the whole US southern division received more than ten test emails. I only discovered the bug when my fellow developer called my attention that he keeps receiving a test email. Fortunately, those email all contains the title 'Test'.

Lesson learned? First, always test your application like you are in the production server. Had I not put 'Test' as the title, it would have been mistaken for a legit email. Second, don't think you already changed your source code somewhere, double check it, if not triple. Oftentimes, you are so sure you have it right, only to found out it's not.

Thursday, November 06, 2008

PHP: How to Print/Echo HTML Tags More Effeciently

PHP is very good at handling strings. But sometimes you have to help PHP do it's job more efficiently. In my recent web development job, I noticed that I always use PHP to print HTML tags. Here's an example of how I would do it before.


<?php
echo "<table>";
echo "<tr>";
echo "<td>"."How to print/echo HTML tags in PHP."."</td>";
echo "</tr>";
echo "</table>";
?>


Now, here's how I do it now which I think is more efficient and HTML readable.

<?php
echo "
<table
<tr>
<td>"."How to Print/Echo HTML tags in PHP More Effeciently"."</td>
</tr>
</table>
";
?>


Notice the difference? In the first PHP sourcecode, I use the echo five times, while in the second one, I use only once.

Using echo to print HTML tags is not expensive in terms of server resources, but if you're using it the way I did in the first example for all your pages and and you have thousands of users to your website, you'll see the difference.

Recent Post