PHP Tip, writing a MySQL query

When writing a MySQL query, it is not necessary to use escaped double quotes (“) when representing literal strings within the query. Single quotes (apostrophes) will work fine. For example, the following two lines of code will perform the same query:

$result = mysql_query(“SELECT foo FROM bar WHERE baz=”test””, $db);
$result = mysql_query(“SELECT foo FROM bar WHERE baz=’test'”, $db);

As will this code perform the same query:

$var = ‘test’;

$result = mysql_query(“SELECT foo FROM bar WHERE baz=’$var'”, $db);

There is not necessarily a performance gain between one method or the other, but using single quotes instead of escaped double quotes greatly improves the readability of your code.

Excuses To Give When You Have Missed Work

~ I can’t come in to work today because I’ll be stalking my previous boss, who fired me for not showing up for work. Okay?

~ I have a rare case of 48-hour projectile leprosy, but I know we have that deadline to meet…

~ I am stuck in the blood pressure machine down at the Food Giant.

~ Yes, I seem to have contracted some attention-deficit disorder and, hey, how about them Skins, huh? So, I won’t be able to, yes, could I help you? No, no, I’ll be sticking with Sprint, but thank you for calling.

~ I just found out that I was switched at birth. Legally, I shouldn’t come to work knowing my employee records may now contain false information.

~ The psychiatrist said it was an excellent session. He even gave me this jaw restraint so I won’t bite things when I’m startled.

~ The dog ate my car keys. We’re going to hitchhike to the vet.

~ I prefer to remain an enigma.

~ I can’t come to work today because the EPA has determined that my house is completely surrounded by wetlands and I have to arrange for helicopter transportation.

~ I am converting my calendar from Julian to Gregorian.

~ I am extremely sensitive to a rise in the interest rates.

~ I refuse to travel to my job until there is a commuter tax. I insist on paying my fair share.

~ I’ve used up all my sick days…so I’m calling in dead!

~ I set half the clocks in my house ahead an hour and the other half back an hour Saturday and spent 18 hours in some kind of space-time continuum loop, reliving Sunday (right up until the explosion). I was able to exit the loop only by reversing the polarity of the power source exactly e*log(pi) clocks in the house while simultaneously rapping my dog on the snout with a rolled up Times. Accordingly, I will be in late, or early.

PHP Tip, echoing text

When echoing text or assigning variables, your script will execute faster if you use single quotes (apostrophes) instead of quotation marks around text which does not contain variables. For example, consider the following two statements:

echo ‘Hello, world, this is a test!’;

echo “Hello, world, this is a test!”;

Execution of the first statement will be faster than that of the second. When PHP sees a literal string surrounded by quotation marks (double quotes), it takes time to interpret the string and determine whether or not it must be expanded by replacing variable names with their contents. PHP does not perform this step when it encounters a literal string expressed within single quotes. Accordingly, strings surrounded with single quotes will not be expanded. In the following example,

$var = ‘good day’;

echo ‘Today is a $var!’;

…The result would be “Today is a $var,” as PHP would not replace the $var with “good day” in the single-quoted string.

Of course, the speed difference is negotiable; we’re talking milliseconds or even picoseconds worth of execution time for either statement on a fast server. However, the speed improvement in large scripts, e.g. those which contain several hundred or several thousand assignment or echo statements, can be noticeable.

PHP Tip, PHP & forms

When an HTML form is posted to a PHP script, PHP automatically converts periods in field names to underscores. For example, if you had form fields called “first.name” and “last.name” you would access them in PHP by using $_POST[‘first_name’] and $_POST[‘last_name’].

Attempting to use the field names with periods, e.g. $_POST[‘first.name’], will not work.

PHP Tip, using date()

When using the date() function, keep in mind that you should use date(“i”) to obtain the minute. A great deal of code incorrectly uses date(“m”) for this, e.g.

$timenow = date(“H:m:s”); //incorrect

Passing “m” to the date() function will return the current month as a number from 1 to 12, not the current minute.

Bumper Stickers

If You Can Read This, I Can Slam On My Brakes And Sue You

Forget World Peace — Visualize Turning Off Your Turn Signal!

HANG UP AND DRIVE!

Where There’s A Will…I Want To Be In It!

Ever Stop To Think, And Forget To Start Again?

This Would Be Really Funny If It Weren’t Happening To Me

If We Quit Voting Will They All Go Away?

This Bumper Sticker Exploits Illiterates

Eat Right, Exercise, Die Anyway

Honk If Anything Falls Off

I Haven’t Lost My Mind – It’s Backed Up On Disk Somewhere

PHP Tips, register_globals

Do you have scripts which suddenly stopped working after you upgraded your PHP version? Chances are, the scripts assumed that register_globals was on (the default for quite awhile) and now that register_globals is disabled by default, the scripts no longer recognize incoming post/get/cookie variables. Add the following lines to your scripts to get them functioning again:

extract($_REQUEST);

extract($_COOKIE);

Crossing the Road

There’s a man trying to cross the street. As he steps off the curb a car comes screaming around the corner and heads straight for him. The man walks faster, trying to hurry across the street, but the car changes lanes and is still coming at him.

So the guy turns around to go back, but the car changes lanes again and is still coming at him. By now, the car is so close and the man so scared that he just freezes and stops in the middle of the road. The car gets real close, then swerves at the last possible moment and screeches to a halt right next him.

The driver rolls down the window. The driver is a squirrel. The squirrel says to the man says,

“See, it’s not as easy as it looks, is it?”

Mellowing Mom

I have five siblings, three sisters and two brothers.

One night I was chatting with my Mom about how she had changed as a mother from the first child to the last.

She told me she had mellowed a lot over the years:

“When your oldest sister coughed or sneezed, I called the ambulance.

When your youngest brother swallowed a dime, I just told him it was coming out of his allowance.”