How to? - My first script using WAMP server and PHP

Posted on June 30, 2014, 1:00 am by about-dev.com


Because a promise is a promise, here is the first post of a long list on the "PHP - How to?" series. I don't want to scare you so I start with a very easy example: code your first PHP script using PHP and WAMP server.

Step 1: install WAMP server
PHP is a server side language, so we need a server where to install the PHP interpreter on and a lot of other technical skills. Because not everyone 
has time to set up servers and testing devs and other stuffs from this area, we can always find an alternative. For the ones using Windows is called WAMP and for the ones using Linux is called LAMP.

In a Linux environment things are much easier, grace to the package manager, that's way I'll start with the Windows environment.

So, because most surely you will want to skip all the above technical steps, you will have to install WAMP server. To do this you have to downloaded it from here. Make sure to choose the latest PHP version available and the proper version for your sistem.

Step 2: start WAMP server
After installation you can start using the server, but to do that you will have to start it by double clicking the W icon. If the icon (that will appear in the right corner of your display, on the task/menu bar) is green everything it's ok and you can start coding. If it is orange or red something went wrong and you have to see what. To see this kind of errors you have to check the Apache error log by clicking the W icon (left click) and accessing the Apache section, then the Apache error log file (youl will see this in the menu that appears).

Most common error is that the 80 port that the server will try, by default to start it's service, is taken by other application. To fix this, go to the Apache server configuration file, httpd.conf (you can find it using the WAMP server interface - look above). In this file, find the following line:

Listen 80

, and change it to an available port. For example:

Listen 81

Then, restart the server using the WAMP server interface, using the "Restart all services" command (menu item). Remember that any action that you'll take in the configuration files for PHP, database or Apache needs a server restart!

By default, Apache runs on the default, 80 port number, as we saw above and that's way it is not in our url. When we change the port, we must put the port number in the url.

For example: suppose we want to execute the contact.php file saved in the document root (C:/wamp/www/ path)

  • with the default, 80, port number the url will look like this: http://localhost/contact.php
  • with the port number changed to 81 for example the url must look like this: http://localhost:81/contact.php

Step 3: let's bake some code
Remember two things:
1. Every PHP file that you will write needs to be saved in the DOCUMENT ROOT path - for WAMP server that is C:\wamp\www (or D, or E or whatever disk you have installed it).
You can access it very easy using the server interface and clicking the "www directory" comand.
2. Every PHP file that you write needs to be executed through the server by accessing the following url:
http://localhost/fileName.php  or  http://localhost/path_to_the_file/fileName.php - "path_to_the_file" starts under the www directory from the document root.

For example I have a project, my_app, saved in the proper place on the server. This project has a index.php file and to access it I'll type in the browser the following url: http://localhost/my_app/index.php

And finally, the code:

/*
 - test.php file
 - this code must be surrounded by one of the PHP available tags; I recommend standard tags
*/
//our first message from PHP
echo "Let's start coding!";

Please check out these resources for a full understanding of what is PHP and what you can do with it:
Introduction to PHP
PHP syntax

Related articles:


Leave a Comment:

User
Email
Website

Blog Search

Popular Blog Categories

Newsletter

Want to be informed about latest posts? Subscribe to our newsletter