PHP Tricks For Beginners

Print Article Print Article

PHP Tricks For Beginners

By Web Hosting Help Guy

In the Web 2.0 era, sites are all about the user – where they’re from, what they like, what they ate for breakfast. It’s becoming increasingly necessary for designers and developers, then, to find a way to tailor the sites they design and maintain to the user. One way to do that is to leverage PHP to change static content into dynamic, user-specific gold.

PHP, at its core, is a server-side scripting language that can be used to customize the user’s Web experience on your site. It’s no surprise, then, that some of the Web 2.0 heavyweights – Digg, Facebook, Wikipedia, WordPress – rely on PHP heavily to tailor content to the specific interests of users. PHP scripts can be used to query the server based on the user’s location, data, or anything else to offer a more personalized experience.

An article that details all the uses of PHP would be as long as the New York City phonebook, and about as exciting to read. And while we have tutorials on PHP planned for the future, we thought we’d start everyone off with six fun and easy PHP tricks you can try right now, no matter what your familiarity with the language is.

1. Referrals

This simple example shows the user how they got to your site.

See It In Action: You reached this site via

Since a user knowing how they got to your site isn’t particularly useful, you need to combine the above script with something, such as a unique page for users who come to your site from a particular location. For example, the script below would send visitors who found your site through Twitter to one page, while all others would be redirected to another. This script, just FYI, needs to go at the very top of the page you’re building.

2. Up-to-date Content

Whether it’s as simple as showing today’s date or as complex as displaying the weather in Bangor, Maine, you can use PHP so users have a customized and up-to-date experience every time they visit your site. You can make PHP seek out dynamic information and show it on your page, giving the user the experience of of a Web page that’s as current as they are.

See It In Action: The current date is February 09, 2012

3. Site Design

Want a site that shows flowers on Mondays, pretty bows on Wednesday, and half-filled martini glasses on Fridays? PHP can help with that. By using PHP to change the look of your site based on factors of your choosing, your site will have a fresh, new appearance for new and returning visitors alike.

Insert this code snippet inside the <head> and </head> tags of your document:

Insert this code snippet inside the element of your document you want to change color of:

See It In Action: Sunday=White, Monday=Orange, Tuesday=Purple, Wednesday=Pink, Thursday=Red, Friday=Blue, Saturday=Green

4. Daily Inspirations

Why not add some reflection time to the busy lives of your site’s visitors? Here’s the process – find some good quotations, and make one appear randomly (or daily) on your site.

See It In Action: Adventure? Excitment? A Jedi craves not these things.

5. Customization and Comments

It’s a fact that people not only want to see websites that reflect what they care about, but also themselves in general. Web 2.0 is all about the user – user-generated content, personalization, and interactivity. PHP makes all this possible. Just think of the uses possible if you ask a user to enter some information – in this case, the sky is the limit.

Creating forms is generally a more complicated use of PHP, so for more information on how to create and use them, you can check out this tutorial:

w3school’s PHP Forms and User Input Tutorial

6. Load Time

This one is a must for developers and system admins. You need to know how long your site takes to load. Nobody sticks around waiting for a site to load anymore – there’s way too much content out there for that. Use this script to find out if your site is taking too long, and if it is, fix it.

Insert this code snippet inside the <head> and </head> tags of your document:

Insert this code snippet directly before the closing </body> tag of your document:

See It In Action:
Seconds To Load: 0.000106

Admittedly, the above examples are simple, but their simplicity is deceiving – by using PHP, any amount of dynamic content is possible. The practical uses abound – shortening links, getting user feedback, and making content user or location-specific. (links) By instructing the server to interact with the client’s information, a savvy web designer or developer can create a community of users where specific tastes are recognized and valued. And, after all, isn’t that what the social media and Web 2.0 revolutions are all about?

See also: 15 Wonderfully Creative Uses For PHP

 

Related posts:

  1. Linux Command Line Tips And Tricks

Comments

abcphp.com January 29th, 2010 at 12:03 am

PHP Tricks For Beginners | Web Hosting Help Guy…

In the Web 2.0 era, sites are all about the user – where they’re from, what they like, what they ate for breakfast. It’s becoming increasingly necessary for designers and developers, then, to find a way to tailor the sites they design and maintain to t…

tvl January 30th, 2010 at 11:08 pm

About the load time, I saw a talk about Pinba yesterday at PHP benelux. It’s still a new project, but it’s a perfect replacement for microtime() timers.

Notable Tech Posts – 2010.02.07 | The Life of Lew Ayotte January 31st, 2010 at 8:09 pm

[...] PHP tricks for beginners [...]

Victor Mesiona December 25th, 2010 at 3:04 am

awesome snippets.. thank you

Tanner February 13th, 2011 at 5:34 pm

Great! Although I already knew how to do this stuff, it’s great for a beginner.

ram chand July 13th, 2011 at 10:53 am

yes, and one more good practice every beginner should do is to keep toolkit in easy access

http://www.websitedesignwebsitedevelopment.com/website-development/toolkit/php-manual-chm/177

this website is not so good but this article is really interesting

Leave A Comment

Name *required

Email *not published *required

Website