Saturday, April 7, 2007

Building Dynamic Web Pages With PHP

By Marian Heddesheimer of Rent A Tutor Site
This article will explain how to build dynamic web pages with PHP. PHP is a very popular and easy to learn script language. Regardless if you are starting to build your website, or if you are already a pro, just read on to learn more about dynamic web pages and PHP.
Why dynamic web pages?
If you have ever created a web site, you already know that maintaining very many pages is really a lot of work. Even if you never need to change the layout of the pages, you want to present the most current information on it. Sometimes the links will change, or the web pages you have linked to will disappear on the Internet. Then you need to load your page again and make all the necessary changes.
These changes are easy to do if your whole website is very small. With 5-10 pages to maintain, you can do it by hand and page by page. With more than ten pages, it will be more difficult. If you want to have a date on each page, you must check if it is the correct one. Moreover, if you ever want to change something in the layout, you have to do this for all the pages.
So this is the big advantage of dynamic web pages: You just need a single template to start with your layout. All content within that template will be filled in dynamically from a script. If the layout has to be modified, there is no need to change every single page, you just modify the initial template and all pages will look alike.
Why PHP?
To build dynamic web pages, you can use other available techniques: Most providers offer server side includes (SSI) for your pages. This will enable you to build one template and fill in parts of the page from include files. The disadvantage with SSI is: If it comes to more complicated stuff, you want to have more power to decide what will be included in your page and what will not. For example, if you want to show a specific content only after a special date and time has passed, you will need some kind of script. With a script you can decide, that the new content will display only if the system date on the web server is set past Jan. 1st 2002 at midnight.
With scripts, you can almost do everything you like. Some kinds of scripts that are offered by the providers are available on the Internet. You can use CGI/Perl for your scripting. Almost every provider supports Perl on the web server. On the other hand, Perl is not so easy to learn and the scripts are not so easy to install. Nonetheless, there are many Perl scripts available for free or a minimal charge that you can use for your own website without knowing the language.
Another script language is ASP, but it is usually only provided on Windows web servers. In the meantime, there is also an implementation of ASP for Linux servers, but it is not yet very popular. Windows web servers are also not very popular, because they are prone to virus attacks so that even Gartner Group has recommended not to use the Internet Server from Microsoft.
A third language is Cold Fusion, but this is not very common and is not often offered by the providers.
So finally, there is PHP: A free script language, what means that it is available free of charges and free of license fees. It runs perfectly on free operating Systems like Linux and works fine with free web server software like Apache. This is one of the reasons, why most providers offer PHP as script language free with the web space.
PHP works fine with HTML: You just put some extra tags in your web page to indicate that it is not a standard HTML, but a PHP code. To tell the web server that the page may contain these special tags, you have to rename your page from "index.html" to "index.php" for example. The file extension .php indicates a PHP script that can also contain standard HTML code. Therefore, it is very easy to mix your existing HTML template with the new PHP script code.
What is PHP?
PHP was developed by Rasmus Lerdorf in 1994 (see details at http://www.phpbuilder.com/manual/intro-history.php). It originally was an interpreter that read one line by another and tried to interpret the written code line by line. Since PHP4, it was improved by the Zend Engine from www.zend.com so that the code is now precompiled. The main advantage is that PHP scripts will now run much faster and the programming is more convenient. Now it is possible to define your functions after using them, which was not doable in PHP3.
PHP was meant as Personal HomePage tool, and this is where the name came from. Later it was renamed to "PHP: Hypertext Preprocessor" which is a reflective acronym because the acronym word "PHP" is contained in the long name. It is meant to be a script language that is easy to use and easy to mix with existing HTML code. Unlike using CGI/perl, there is no need to supply a special directory on the web server like cgi-bin. In most cases, there is no need to change access rights on the files that contain PHP code. The only necessary change to the HTML file is to modify the file extension from .html to .php. Some providers offer the old version PHP3 along with the new PHP4 so you have to use sometimes .php4 or .php3 for your filenames. Nonetheless, the official extension is .php, so you may have to ask your provider if there is something special on your web space.
As mentioned before, PHP is completely free and open source. This means that even the source code for PHP itself is available on the Internet. It is written in C and can be modified when necessary. Many developers are still working on the code, releasing new versions from time to time.
How to Start?
Got excited about PHP yet? So you want to know where to start? The initial start is quite easy: You should first check out if PHP is available with your web space and which version it is. It is really easy to do this. Just create a simple text file that contains theses commands:

Save this file as "info.php" and upload it to your web space. Then call it in your favorite browser by entering the appropriate URL like "www.mydomain.com/info.php".
You will get a page with lots of information or you will just see your command in the browser. If you just see the single line, either your provider does not support PHP or they are using other file extensions. Then try to rename your file to "info.php4", "info.php3" or "info.phtml". These are the most common extensions used instead of the official .php extension.
If you got the whole PHP information page, you will see the version of PHP, all the settings and server variables that are available for PHP and within your own scripts. It is a good idea to print this information for later reference.
Before you can build dynamic web pages, you should acquire a basic understanding about the language. If you already know any programming language like C, C++, VisualBasic or Java, you may find it very easy to switch to PHP. The language is pretty much like C++ and Java so that you can migrate from these languages. But even if you just have some knowledge in VisualBasic, you know what a variable and a function is, so it is not difficult to learn PHP. If you don't know anything about programming, PHP is a great tool in order to learn your first programming language for web programming.
To build your first program, you can use a simple text editor, even if it is not very comfortable. If you write more than ten lines of code, you might want a more convenient tool that can show you the correct spelling of the keywords. This feature is also known as "syntax highlighting" and there are some tools available for free. A list of these editors is available at: http://www.itworks.demon.co.uk/phpeditors.htm.
Another list with editors for PHP can be found at: http://php.weblogs.com/editors.
The next step will be to learn the specific language and create a first project. A good reference is the official php web site at http://www.php.net. Another good source for information is http://www.phpbuilder.com, where you can find articles on common PHP solutions and a forum with solutions to various questions on PHP. You will also find a variety of tutorials on PHP and there are several newsgroups related to PHP programming. At least, there are some online classes available for PHP like the one that I offer at http://www.rent-a-tutor.com/courses/index.php.

No comments: