Knowledgebase
Dynamic Web Pages: Intro to PHP
Posted by Clarke M on 03 April 2006 07:55 PM
Did you ever wish your web site could..
* Change automatically with the seasons.
* Display content from a database.
* Store customer input in a database.
* display different content depending on who was viewing it.
* be an online store.

If so you may wish to make use of some of the tools that WTC Communications offers on our webspace.

One of these is called PHP.
When someone visits any page on your website that has the extension '.php' the file will be examined to see if you have included any special instructions, then it will be constructed according to your commands and displayed to the visitor.

Want to try it out?
Lets start with a test page.
Open Notepad
type the following:
<?php
phpinfo();
?>

save the file as test.php
upload it to your website.
Surf to your new page:
http://www.mydomain.com/test.php
If you get a blank page:
* check to ensure that it is typed just as shown above.
* ensure that notepad did NOT save it as test.php.txt

If you were successful you will be looking at a display of all the php options and settings that relate to our hosting platform.

Ready to try something more challenging?

First item on the agenda now is to get ready to make mistakes!
Yep, you are going to have 'bugs in your code'- In english that means that you can expect to have typos, spelling mistakes and other errors in the instructions that you type.
One way to help fix these problems is to have the web site tell you what it knows about them. Some of the error messages are slightly less that helpful. For example; when it just says something like 'error!' but often it will lead you right to the solution.

Normally our web hosting computer does not display php error messages because most people who visit your web site are not interested in knowing about them and lets face it ... its none of their business anyhow. we don't make mistkaes. icon_smile.gif

Since no one knows how to find this new page we are making lets risk our reputation and turn on error reporting.
Open notepad and type the following

php_flag display_errors on
php_value error_reporting 7

If your computer allows you to, save it with the file name '.htaccess'
If you cannot use a file name that includes dots('.') save it without the dot and rename it after you upload it to the web.
Once this .htaccess file is located in the public folder on your web site you are ready to start experimenting with php.

How about having a special weekend(or Summer or Christmas etc) version of your site?
If you have a special folder where you keep your weekend images your site could automatically use these on the weekend.
You could change the text also.

Make a copy of one of your web pages and change its extension from '.html' or '.htm' to '.php'
Open your page in a text editor, Notepad will work OK.
At the top of you page above where you see '<html>' type the following. You can safely omit lines starting with '//' if you wish.

<?php
// php ignores any info that follows two slashes
// ..and will only start again when it comes to a new line.
// so it can be used for notes to self.
// Note: That "l" in the line below is a lower-case "L"
If (date("l")=="Saturday" or date("l")=="Sunday"){
$text_message = "Weekend version";
$image_folder="/images/weekend/";

}else{
$text_message = "regular version";
$image_folder="/images/";
}

?>

Now you can use the information you have stored in $greeting and $image_folder in your web page.
for example your graphics could be done like this.
<img src="<?=$image_folder?>heading.gif">

To display a custom message You could insert a statement like
<h1>You are viewing the <?=$text_message?> of my site </h1>

Or you could use the 'echo' command:

<?php
echo "You are viewing the $text_message of my site";
?>

If you were successful php will replace items like '$text_message' with the information that you stored under that name giving your visitors a customised web page.

Want more?
Visit http://www.google.ca and do a search for php
The 360,000,000 responses should be enough to satisfy your curiosity icon_wink.gif

I suggest you start with http://www.php.net/

enjoy!
(630 vote(s))
This article was helpful
This article was not helpful

Comments (0)
Help Desk Software by Kayako Fusion