Archive for June, 2005
Thursday, June 30th, 2005
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 [...]
Posted in PHP Tips | Comments Off
Thursday, June 30th, 2005
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 [...]
Posted in Just Jokes | Comments Off
Wednesday, June 29th, 2005
If you need to determine the number of elements in an array, you can use either sizeof() or count():
<?
$foo = array(“one”, “two”, “three”);
echo count($foo); //will output 3
echo sizeof($foo); // will output 3
?>
Posted in PHP Tips | Comments Off
Wednesday, June 29th, 2005
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 [...]
Posted in Just Jokes | Comments Off
Tuesday, June 28th, 2005
If you find yourself working on a project that deals with dates – for example a calendar or an accounting package – you may need to determine the time at the start of the current week. While there are several ways to do this, the following one-liner will give the necessary value:
$result = mktime(0, 0, [...]
Posted in PHP Tips | Comments Off
Tuesday, June 28th, 2005
About 90 fifth-graders piled into the airliner I was flying, on their way home from a school trip.
Once we were in the air, and the crew began serving drinks, I could hear them pleading with the children to settle down and let the other passengers get some sleep.
No amount of reasoning seemed to help, until [...]
Posted in Just Jokes | Comments Off
Monday, June 27th, 2005
By default, the PHP binary will output HTTP headers when you run a PHP script from the command line. While this can be useful if you’re trying to debug a script which sends custom headers, the headers are often unnecessary at the shell. To suppress them, specify the -q flag to the `php` command.
Posted in PHP Tips | Comments Off
Monday, June 27th, 2005
Tom was so excited about his promotion to Vice President of the company he worked for and kept bragging about it to his wife for weeks on end.
Finally she couldn’t take it any longer, and told him, “Listen, it means nothing, they even have a vice president of peas at the grocery store!”
“Really?” he said. [...]
Posted in Just Jokes | Comments Off
Saturday, June 25th, 2005
Sometimes you may require a list of all of the files in a directory. Such a list can be built using the opendir() and readdir() functions:
<?php
$targetdir = “/var/tmp”;
$files = array();
$directory = opendir($targetdir);
while($filename = readdir($directory)){
#ignore . and ..
if(strlen($filename) > 2){
array_push($files, $filename);
}
}
?>
Upon execution, $files will be an [...]
Posted in PHP Tips | Comments Off
Friday, June 24th, 2005
If you need to keep track of users’ passwords for authentication – for example, if members have to login to your site – consider storing their password in an encrypted format instead of plaintext. This way, if your database somehow becomes compromised, the passwords for your user accounts are still somewhat safe.
One way to accomplish [...]
Posted in PHP Tips | Comments Off
Friday, June 24th, 2005
A young man called his mother and announced excitedly that he had just met the woman of his dreams. Now what should he do?
His mother had an idea: “Why don’t you send her flowers, and on the card invite her to your apartment for a home-cooked meal?”
He thought this was a great strategy, and a [...]
Posted in Just Jokes | Comments Off
Thursday, June 23rd, 2005
If you need to print out a large amount of text or HTML, consider using heredoc notation instead of numerous echo statements. For example, the following code:
echo “This is a test, and this is line 1″;
echo “This is a test, and this is line 2″;
echo “This is a test, and this is line 3″;
echo “This [...]
Posted in PHP Tips | Comments Off
Thursday, June 23rd, 2005
Did you hear about the doctor who wrote out a prescription in the usual doctor’s fashion?
The patient used it for two years as a railroad pass.
Twice it got him into Radio City Music Hall, and once into Yankee Stadium.
It came in handy as a letter from his employer to the cashier to increase his salary.
And [...]
Posted in Just Jokes | Comments Off
Wednesday, June 22nd, 2005
Ever wanted to create a generic web form handler, which can email you the results from all of your site’s forms, similar to the infamous FormMail.cgi? If you have multiple forms and find yourself without the time to write a handler for each one, try using the following script:
<?php
#generic form mailer
while(list($key, $val) = each($_POST)){
[...]
Posted in PHP Tips | Comments Off
Wednesday, June 22nd, 2005
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 [...]
Posted in Just Jokes | Comments Off
Tuesday, June 21st, 2005
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 [...]
Posted in PHP Tips | Comments Off
Tuesday, June 21st, 2005
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 [...]
Posted in Just Jokes | Comments Off
Monday, June 20th, 2005
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() [...]
Posted in PHP Tips | Comments Off
Monday, June 20th, 2005
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 [...]
Posted in Just Jokes | Comments Off
Sunday, June 19th, 2005
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 [...]
Posted in PHP Tips | Comments Off
Saturday, June 18th, 2005
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 [...]
Posted in PHP Tips | Comments Off
Friday, June 17th, 2005
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.
Posted in PHP Tips | Comments Off
Friday, June 17th, 2005
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?
Posted in Just Jokes | Comments Off
Thursday, June 16th, 2005
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 [...]
Posted in PHP Tips | Comments Off
Thursday, June 16th, 2005
A couple was arranging for their wedding, and asked the bakery to inscribe the wedding cake with “1 John 4:18″ which reads “There is no fear in love, but perfect love casts out fear.”
The bakery evidently lost, smudged or otherwise misread the noted reference, and beautifully inscribed on the cake “John 4:18″ …
“for you have [...]
Posted in Just Jokes | Comments Off
Wednesday, June 15th, 2005
I have my own system for labeling homemade freezer meals.
Forget calling them “Veal Parmigiana” or “Turkey Loaf” or “Beef Pot Pie.”
If you look in my freezer you’ll see “Whatever,” “Anything,” “I Don’t Know,” and, my favorite, “Food.”
That way when I ask my husband what he wants for dinner, I’m certain to have what he wants.”
Posted in Just Jokes | Comments Off
Tuesday, June 14th, 2005
A guy was on the side of the road hitchhiking on a very dark night and in the middle of a storm. The night was rolling on and no car went by. The storm was so strong he could hardly see a few feet ahead of him.
Suddenly he saw a car coming toward him and [...]
Posted in Just Jokes | Comments Off