Fun PHP Tricks for Beginners

To the novice web developer, PHP can be a pretty intimidating scripting language with a bit of a tough learning curve. However, one of the best ways to combat the overwhelming feeling that many new web developers feel when dipping their toes in PHP is to just get your hands dirty and play around with it. By using these PHP tricks, you can add exciting content to your site and, in the meantime, discover and learn why the code produces the results that it does.

1. Daily Inspirations

Using this simple, but effective code snippet, you can rotate random quotes or other pieces of information in a certain area on your site. You can adjust the code to show a random quote each time the page is refreshed, or you can control the quote being changed at random once during each day. Visitors will notice your changing content every day, and it will keep the page exciting and refreshing without the same, old boring text day in and day out.
Insert this code into your document:

$quote = array(
1 => “Quote 1″,
2 => “Quote 2″,
3 => “Quote 3″,
4 => “Quote 4″,
5 => “Quote 5″,
);
srand ((double) microtime() * 1000000);
$randnum = rand(1,5);
echo”$quote[$randnum]“;

2. Load Time

With PHP, your options are seemingly limitless, and you can even use it to your advantage to troubleshoot any loading issues on your site. For example, you can generate and disperse a few code snippets in your file that will notify the page viewer how long it took to load the web page. This turns out to be a pretty nifty tool if you’re trying to waddle down on unnecessary items that can cause your page to load slowly.
First, insert this code somewhere between the tags of your document:

$starttime = microtime();
$startarray = explode(” “, $starttime);
$starttime = $startarray[1] + $startarray[0];
Next, you will want to inject this code just before the closing tag. It is imperative to make sure that it is the last bit of code just before the closing tag, or you might experience problems.
$endtime = microtime();
$endarray = explode(” “, $endtime);
$endtime = $endarray[1] + $endarray[0];
$totaltime = $endtime – $starttime;
$totaltime = round($totaltime,10);
echo “Seconds To Load: “;
printf (“%f\n”, $totaltime);

3. Customization and Comments

One of PHP’s greatest assets is its ability to allow a site to connect on a much more versatile and efficient level to the user. Due to this, you’re going to probably, at some point, want to include some sort of user input or forms on your site. Because it is a lengthy and more in-depth process than simply cutting and pasting some code, it’s best to check out this tutorial on creating forms and user input areas.
W3school’s PHP Forms and User Input Tutorial (link: http://www.w3schools.com/php/php_forms.asp)

4. Referrals

Another cool little trick that you can use on your site is a script that displays the referrer that sent you to your site. For example, if a user came from Google to your site, they’ll be able to see a small section on your site that logs where they came from. The text from this code will appear as, “You reached this site via [referrer].”

$referer = $_SERVER['HTTP_REFERER'];
echo “You reached this site via ” . $referer;

5. Up-to-Date Content

Have you ever wondered how some news sites keep constant tabs on the weather? If you were to write code manually for updated weather, you would be at your computer at all times. Luckily, thanks to PHP, you can use a particular snippet of code to show various updated information. For example, if you wanted to show the time or the current weather in New York City, you could place this code on a certain area of your site. It would update automatically, and you’d never have to touch the code again.

$date = Date(“F d, Y”);
echo “The current date is ” . $date;

6. Site Design

Have you ever wondered how some sites seem to have a certain “look” for every day of the week? For example, the site may be themed with dogs on Wednesdays, while the site might take on a party theme on Fridays. This would be exhausting having to manually change the code for the site every day just to change out the various themes. Fortunately, you can easily pull something like this off by using PHP.
Begin by inserting this code between the tags in your document:

$day = date(“w”);
$color = array(“white”, “orange”, “purple”, “pink”, “red”, “blue”, “green”);
Next, you will want to inject this code snippet into the element that it will apply to:
print(“style=\”color:$color[$day];\”");

Using these examples in your own work, you can easily discover just how much of your site you can manipulate and control just by using PHP. Give some of these a try on your own site to experience just how much customization and power this revolutionary scripting language can give you.

0 comments:

Post a Comment