|
Introduction: Now scripting on the internet was something which very few people even attempted, let alone mastered. Recently though, more and more people have been building their own websites and there scripting languages have become more important for the web site development process and also for web developers. Because of this, scripting languages are becomming easier to learn and PHP script is one of the easiest and most powerful yet.
What Is PHP?
PHP is stands for Hypertext Preprocessor and is a server-side scripting language. This means that the PHP script is run on your web server, not on the user's browser, so you do not need to worry about compatibility issues of these languages. PHP is relatively new (compared to languages such as Perl (CGI) and Java) but is quickly becomming one of the most popular scripting languages on the internet.
Why PHP?
You may have to wondering why you should choose PHP over other scripting languages such as Perl or even why you should learn a scripting language at all. We will deal with learning scripting languages first. Learning a scripting language, or even understanding one of them, can open up huge new possibilities for your website development process. Although you can download pre-made scripts from sites like Hotscripts, these will often contain advertising for the author or will not do exactly what you want. With an understanding of a web development scripting language you can easily edit these scripts to do what you want, or even create your own scripts.
Using web development language scripts on your website allows you to add many new 'interactive' features like feedback forms, guestbooks, message boards, counters and even more advanced features like portal systems, content management, advertising managers etc. With these sort of things on your website you will find that it gives a more professional image. As well as this, anyone wanting to work in the site development industry will find that it is much easier to get a job if they know about a scripting language.
What Do We Need?
As mentioned at earlier tutorial pages, PHP is a server-side scripting language. This means that, although your users will not need to install new softwares, your web host will need to have PHP script language set up on their server. It should be listed as part of your package but if you don't know if it is installed you can find out using the first script in this tutorial. If you server does not support PHP you can ask your web host to install it for you as it is free to download and install. If you need a low cost web host which supports PHP we would recommmend HostRocket.
Writing PHP Language
Writing of PHP on your computer is actually very simple process. You don't need any specail software, tool and except for a text editor (like Notepad in Windows). Run this text editor and you are ready to write your first PHP language script.
Declaring PHP
The PHP scripts are always enclosed in between two PHP scripting tags. This tells your server to parse the information between them as PHP script. The three different forms are as follows:
<?
PHP Code In Here
?>
<?php
PHP Code In Here
php?>
<script language="php">
PHP Code In Here
</script>
All of these tags work in exactly the same way but in this tutorial we will be using the first option (<? and ?>). There is no particular reason for this, though, and you can use either of the options. You must remember, though, to start and end your code with the same tag (you can't start with <? and end with </script> for example).
Your First Script
Your first PHP script that you will be writing is very basic as scripting language mentioned. All it will do is print out all the information about PHP on your server page. Type the following code into your text editor:
<?
phpinfo();
?>
As you can see this actually just one line of your code. It is a standard PHP script function called phpinfo which will tell the server to print out a standard table of information giving you information on the setup of the server language.
One other thing you should notice in this example is the line ends in a semicolon. This is very important. As with many other scripting and programming languages nearly all lines are ended with a semicolon and if you miss it out you will get an error at runtime.
Finishing and Testing Your PHP Script
You may have finished your PHP script save it as phpinfo.php and upload it to your server in the normal way to store. Now, using your browser, go the the URL of the PHP script. If it has worked (and if PHP is installed on your server) you should get a huge page full of the information about PHP on your web server.
If your tested PHP script doesn't work and a blank page displays, you have either mistyped your code or your server does not support this function (although we have not yet found a server that does not). If, instead of a page being displayed, you are prompted to download the file, PHP is not installed on your server side and you should either search for a new web host or ask your current host to install PHP script.
This will a good idea to keep PHP script for your future reference. |