Unlike an HTML document, which can be run in the web browser from your own hard drive, a PHP script must be run on a machine with web server software and PHP installed.
Therefore to run your PHP scripts you must either ...
Upload your scripts to a hosting service that has PHP installed.
Install web server software and PHP on your own machine and run them there.
Ultimately you will wish to run your scripts on an online hosting service but it's nice if you can run them on your own machine whilst you develop them.
Installing Apache Web Server
Apache is the most used web server software on the Internet and is open source and free. It will allow you to run your own machine as a web server, which will enable you to run PHP and other server side languages such as MySQL.
You can download Apache by going to www.apache.org and clicking on the Download link to find the most appropriate version for your system.
Once installed it's straightforward to edit the Apache configuration file to allow you run web pages from your chosen directory with http://localhost/ in the address box of your browser.
Do this by going to your Start menu and finding Apache HTTP server > Configure Apache Server > Edit the Apache httpd.conf Configuration File.
This will open the config file in Notepad and to find the place to edit do a search for "documentroot". Here you should find a section that normally says by default DocumentRoot "C:/Program Files/Apache Group/Apache/htdocs".
This is the path to the directory on your hard drive from where Apache will serve files. You can change this path to a directory of your choosing for example DocumentRoot "C:/Documents and Settings/User/My Documents".
Next scroll down until you find the place that normally says by default <Directory "C:/Program Files/Apache Group/Apache/htdocs">.
This path must be changed to the path that you used above e.g. <Directory "C:/Documents and Settings/User/My Documents">.
Note: Rather than overwrite the original path it's preferable to comment out each path by placing a # character before each then entering the new path below.
For the change to take effect you will have to restart Apache. If Restart Apache is available as a choice from the
Start menu > Apache HTTP server then you can do it this way. If not then you will have to reboot your machine.
Installing PHP
PHP requires a web server such as Apache so once you have this installed go to www.php.net/downloads.php and download and install the most up to date version for your system.
If you have any problems then consult www.php.net/manual/en/ and choose the installation instructions for your system.
Running on a Hosting Service
If you don't wish to install Apache or PHP then check if your hosting service supports PHP by creating the following script in your text editor, saving it as php_info.php and uploading it to the document root.
The PHP info Script
<?php
phpinfo();
?>
Enter your domain name into your web browser followed by /php_info.php and hit Return. If your web host has PHP installed then you will see a list of information about the version of PHP. If nothing happens, and you're sure you haven't made a mistake, then your web host doesn't support PHP.
If this is the case then you will either have to switch to a host that does support PHP or install it on your own machine.
Your First PHP Script
You're now ready to create and run your first proper PHP script and it will use the PHP echo() function. The echo() function, which will be covered in more detail in a later tutorial, writes content to the screen so will be used a lot from now on.
Your First PHP Script
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html> <head> </head> <body>
<?php
echo("This is my first PHP script");
?>
</body> </html>
When you run the script from the browser it should output the text that you placed within the echo() function.
If you view the HTML source of the page you'll see that the PHP tags and the echo() function have been removed, leaving only the text. The PHP engine always removes all PHP instructions leaving only the output if there is any.
In the script example above notice that PHP statements must end with the semicolon character or an error will be produced (the exception is when it is the last statement before the PHP closing tag).
Since PHP ignores white space and carriage returns, the ; character is required for the PHP engine to know where each statement begins and ends.