The Haircut

A young man had just gotten his driving permit. He asked his father, who was a minister, if they could discuss his use of the car.

His father said to him, “I’ll make a deal with you. You bring your grades up, study your bible a little, and get your hair cut, then we will talk about it.”

A month later the young man came back and again asked his father if they could discuss his use of the car.

His father said, “Son, I’m real proud of you. You have brought your grades up, you’ve studied your bible diligently, but you didn’t get a hair cut!”

The young man waited a moment and replied, “You know dad, I’ve been thinking about that. You know Samson had long hair, Moses had long hair, Noah had long hair, and even Jesus had long hair.”

His father replied, “Yes son, and they walked everywhere they went!”

PHP Tips building web forms

Sometimes when building web forms, you may need to collect multiple values for a single field. Perhaps you have a form which gives a list of common hobbies, and visitors can check all hobbies which interest them. But how do you get PHP to realize that the user has made multiple selections? All you need to do is append square brackets to the end of the field name in your HTML.

For example,

<input type=”checkbox” name=”hobbies[]” value=”Computers”>Computers
<input type=”checkbox” name=”hobbies[]” value=”Fishing”>Fishing
<input type=”checkbox” name=”hobbies[]” value=”Hiking”>Hiking

If a visitor selects all three checkboxes, when the form is posted, your script will have an array named $_POST[hobbies] which contains the three hobbies as its elements.

Who Am I?

Years ago, when our daughters were very young, we’d drop them off at our church’s children’s chapel on Sundays before the eleven o’clock service.

One Sunday, just as I was about to open the door to the small chapel, the minister came rushing up in full vestments. He said he had an emergency and asked if I’d speak to the children at their story time. He said the subject was the Twenty-third Psalm.

But just as I was about to get up from the back row and talk about the good shepherd, the minister burst into the room and signaled to me that he would be able to do the story time after all.

He told the children about sheep, that they weren’t smart and needed lots of guidance, and that a shepherd’s job was to stay close to the sheep, protect them from wild animals and keep them from wandering off and doing dumb things that would get them hurt or killed.

He pointed to the little children in the room and said that they were the sheep and needed lots of guidance.

Then the minister put his hands out to the side, palms up in a dramatic gesture, and with raised eyebrows said to the children, “If you are the sheep then who is the shepherd?” He was pretty obviously indicating himself.

A silence of a few seconds followed. Then a young visitor said, ” Jesus, Jesus is the shepherd.”

The young minister, obviously caught by surprise, said to the boy, “Well, then, who am I?”

The little boy frowned thoughtfully and then said with a shrug, “I guess you must be a sheep dog.”

PHP Tip using flush()

You may have noticed that scripts which take a long time to execute often display nothing but a blank “loading” page in the web browser until they finish running. If you find yourself in this situation with a script you’ve written, it’s possible to make the script output incrementally. To do this, use the flush() command. flush() will force the script to send any data in the output buffer to the browser immediately, instead of waiting until execution terminates.

For example, suppose you have a newsletter with several thousand subscribers and because each message contains a unique unsubscription link, the mails must be sent to one recipient at a time. Unfortunately, your script takes upwards of 10 minutes to run, and you’re never sure how far along it is until the entire thing finishes. This can be solved by adding flush() into the loop, to print out a realtime status message after every 100 emails:

$result = mysql_query(‘SELECT DISTINCT email FROM newsletter_subscribers’, $db);
while($myrow = mysql_fetch_array($result)){
$body = ” … body plus unsubscription link would go here … “;
mail($myrow[email], ‘This Month’s Newsletter’, $body, ‘From:’);
if($tick % 100 == 0){
echo “$tick messages have been sent…<br>”;

Once you use flush() a few times, you’ll start finding more and more places where it comes in handy!

Murphy's Laws for Parents

1. The tennis shoes you must replace today will go on sale next week.

2. Leakproof thermoses — will.

3. The chances of a piece of bread falling with the grape jelly side down is directly proportional to the cost of the carpet.

4. The garbage truck will be two doors past your house when the argument over whose day it is to take out the trash ends.

5. The shirt your child must wear today will be the only one that needs to be washed or mended.

6. Gym clothes left at school in lockers mildew at a faster rate than other clothing.

7. The item your child lost, and must have for school within the next ten seconds, will be found in the last place you look.

8. Sick children recover miraculously when the pediatrician enters the treatment room.

9. Refrigerated items, used daily, will gravitate toward the back of the refrigerator.

10. Your chances of being seen by someone you know dramatically increase if you drive your child to school in your robe and curlers.

PHP Tip datestamps timestamps

Are you struggling with “date math?” If you’ve fallen into the trap of converting string-based timestamps into month, day, year, hour, minute, and second, and then trying to perform some sort of math or calculations based upon these values, consider storing timestamps in epoch format instead. Today’s tip is a bit longer than usual but you might find it worth the extra read!

“Epoch” time represents the number of seconds which have elapsed since midnight, January 1st, 1970 GMT. Because the format is based upon a value which has some meaning in terms of time – seconds – calculating time becomes a snap compared to calculating time with string values. As an example, suppose you currently store datestamps in string format, and you have an existing stamp, e.g. “2003-03-04 13:56:11.”

To find the date and time 8 hours after that stamp, you’d need something like this:

$stamp = “2003-03-04 13:56:11”;
list($date, $time) = split(‘ ‘, $stamp);
list($year, $month, $day) = split(‘-‘, $date);
list($hour, $minute, $second) = split(‘:’, $time);

#Add 8 hours
if(($hour + 8) <= 23){
$hour += 8;
#We got lucky!
die(“$year-$month-$day $hour:$minute:$second”);
#A rather large amount of code would go here,
#determining whether it’s necessary to add
#a month and year, whether or not we’re dealing
#with a leap year, etc.

If you’re doing things that way, you’re punishing yourself entirely too much. Here’s how it works using epoch stamps. Let’s start with the same time – 13:56:11 on March 3rd 2003 – and add 8 hours:

$stamp = 1046807771;
echo date(“Y-m-d H:i:s”, $stamp + (3600 * 8));

And there you have it, we know that 8 hours from our starting point would be 2003-03-04 21:56:11. No muss, no fuss, no bother. Another example, this one perhaps a bit more practical, is determining whether or not a specific event happened within a certain time period. Suppose your website tracks customer logins in a database, and automatically forces logins more than 24 hours old to expire. This, too, is easier with epoch:

$lastlogin = 1046807771;
if($lastlogin < (time() – 86400)){
die(“Sorry, your login has expired!”);

The time() function returns the current epoch timestamp. By comparing the last login stamp to the result of time() minus 86400 – the number of seconds in a day – we can determine whether or not the last login occurred more than 24 hours ago.

Aside from making date calculations simpler, there are three other advantages to using epoch time as opposed to string-based timestamps:

First, most (if not all) unix systems inherently support the epoch format and use it to track time internally; epoch time is often referred to as “unix time” for this reason.

Second, because epoch time is measured according to GMT, the integer representing the accurate epoch time is exactly the same everywhere in the world. This eliminates some timezone confusion and provides for more portability between different servers in different timezones. Of course, the current epoch time has different meaning in different time zones, our example of 1046807771 would have been 11:56 AM in the Pacific timezone and 2:56 PM on the east coast.

Finally, on some systems, an epoch time representation uses less storage space than a string-based one. Consider the difference in size between the integer 1046807771 and the string “2003-03-04 13:56:11.” In MySQL, an integer (such as an epoch value) takes only 4 bytes of storage[1], whereas a datetime value requires 8 bytes, and a timestamp stored as a varchar could require 15-20 bytes. Storing timestamps as integer(10) fields instead of datestamp or varchar fields in your database can save space.

[1] MySQL’s timestamp data type, which displays as YYYYMMDDHHMMSS, is stored as an epoch value and requires only 4 bytes as well. If all of your date math will be done within SQL queries, use the timestamp data type instead of storing epoch stamps in integer fields.

PHP Tip uniqid() function

If you have a need for a unique string, consider using PHP’s built in uniqid() function. uniqid() will return a string value based upon the current system time, comprised of both letters and numbers, such as “3e5f173a6d6ed.”

This can be useful in a number of situations:

– Creating unique order IDs

– Generating default passwords

– Naming temporary files

When you call uniqid(), you must pass a string argument, though an empty string (” or “”) is sufficient. If you pass a non-empty string, it will be prepended to the return value. For example,

$fp = fopen(‘/var/tmp/’ . uniqid(‘tmp’) . ‘.file’, ‘wb’);

…would create a resource handle to a file with a name such as “/var/tmp/tmp3e5f2151b0fd0.file.”

The string returned by uniqid() is unique to the microsecond at which it was generated. In other words, if two scripts on your server call uniqid() at the exact same microsecond, they may receive the exact same return value. However, because there are one million microseconds in a second, this degree of uniqueness is more than enough for most applications.

PHP Tip using the rand()

Before using the rand() function, it’s a good idea to seed the random number generator first. This can be done with the srand() function. Its most common form is:

srand((double)microtime() * 1000000);

If you don’t take this step before using rand(), it’s possible – likely, even – that your “random” numbers will not necessarily be random.

Visiting Friends

Hush-A-Bye Buddy
In our guest room
It’s been three weeks now —
Are you leaving us soon?
We’ll all miss your snoring
And carrying on,
But please–
Won’t you go back
To Boca Raton?

PHP Tip file()

If you want to read the contents of a file into a variable, it is not necessary to use the fopen(), fread(), and fclose() commands. Instead, take advantage of PHP’s file() command, which will read a file into an array which is automatically split on newlines:

$text = file(“text.txt”);

If you’d rather have the file read into a string variable instead of an array, use the implode() command in conjunction with file() :

$text = implode(“”, file(“text.txt”));

Supposing that the file text.txt contains the following four lines:


…the first example would create $text as an array with four elements, element [0] holding “Thisn” and element [3] holding “test.” The second example would create $text as a string, holding the value “Thisnisnantest.”