Fun PHP Tricks For Beginners
PHP can be intimidating scripting language to the novice developer with a bit of tough learning angles. Nevertheless, one of the best ways to handle this feeling is to get your hands dirty and play around with it. Here are some PHP tricks that can add some excitement while coding a website.
1. Daily Inspirations
Use this effective snippet to 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. This keeps pages refreshing and exciting by changing content every day, it‘ll also help in getting visitors to your site.
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 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
PHP allows a site to connect on a much more versatile and efficient level to the user. Due to this,at some point, you may 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.
4. Site Design
You have seen some website where themes change every day to give a fresh look to the website. Now you are able to do it easily, just follow this tricks 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.



