For training call +44(0)1273 6222 72

Your basket is empty View Cart shopping cart

theBLOG

PHP Pointers for Beginners

/ home / blog /PHP Pointers for Beginners

Written by Guest Author – Mon 31 Oct 2011

PHP Pointers for Beginners

PHP elephantWith Web 2.0, website visitors expect a full-featured custom website that uses their location and other information to display a unique website. As a developer or designer, you want to find ways to make your website a unique experience for each visitor. You can use PHP to create a feature-rich, dynamic website for your visitors.

PHP is a scripting language embedded within HTML. A PHP processor module, which reads the script, is located on the web server. The module renders a readable web page, so site visitors do not need special software installed on their own computer to view features on the page. Used on over 20 million web pages, PHP is one of the most popular languages used to create dynamic web pages.

A few of the most popular Web 2.0 websites use PHP to create customized content for their visitors. Facebook, WordPress, Digg and Wikipedia all use PHP to produce websites tailored to each visitor's needs and interests. Web developers can use PHP scripts to pull information from the database about each user, including location and previously saved data.

PHP has many features you can use to customize your website, but listing all of them would make this article too long ,not to mention too boring, to read. You will learn more techniques in future articles, but this article will introduce you to six easy ways to use PHP, even if your experience with PHP is limited.

Website Appearance

You can change the appearance of your web page depending on the day or any other factor. For example, show a picture of the sun during the day and a picture of the moon at night. This keeps your website fresh for returning visitors and keeps it interesting.
Place this code between the head tags in the HTML code:

$day = date("w");
$color = array("white", "orange", "purple", "pink", "red", "blue", "green");

Place this piece of code inside the and tags of your HTML to change the color:

print("style=\"color:$color[$day];\"");

Load Time

As a developer, this is vital information for your website. Web visitors are impatient and expect a page to load quickly, or they will leave before the page loads. This script lets you see how long it takes for your page to load.

Place this code between the header tags within the HTML:

$starttime = microtime();
$startarray = explode(" ", $starttime);
$starttime = $startarray[1] + $startarray[0];

Place this piece of code right before the tag within the HTML code:

$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);

Current Content

This script shows current information to your visitors. For example, you can display the local weather for each visitor based on their IP address. You can use a script that recalls current information from other websites, so each visitor gets a unique experience on yours.

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

Daily Quotes

Here's how to make a new quote or other text display randomly on your website:

$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]";

Web Page Customisation

Since Web 2.0 is all about personalized content and an interactive website, use PHP and forms to let users enter their own information for future reference. This is more complicated than other scripts we will mention here, but this website has an excellent tutorial:

http://www.w3schools.com/php/php_forms.asp

Visitor Referrals

The following code tells visitors where they came from:

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

This script is fun, but not very informative for your visitors. You can, however, use this information and PHP to direct visitors to a custom page depending on how they ended up on your website. You can create pages for visitors based on where they came from, so visitors from Google will see a different page than visitors from Facebook or Twitter.

Place the following script at the top of your page:

if (strlen(strstr($_SERVER["HTTP_REFERER"],"twitter"))>0) {

header ('Location: http://' . $_SERVER['HTTP_HOST'] . '/from_twitter.html');

} else {

header ('Location: http://' . $_SERVER['HTTP_HOST'] . '/not_from_twitter.html');
}

Ending Thoughts...

These examples are very simple, but they show how you can create a dynamic website using PHP. PHP tells the server to use previously entered information in the database and other information available about the visitor to display a unique, custom page for your visitors. As a developer, you can create a unique, custom site for each visitor, which is what Web 2.0 users have come to expect.

Posted under:

Back
blog comments powered by Disqus