We now know that PHP is run on the server whose role is to generate pages. However, only a server can read PHP, and your computer is not a server. So how will you create a dynamic website if PHP doesn’t even work on your computer ?
There’s no need to worry : we’ll temporarily transform your computer into a server so that you can work on your dynamic site. You’ll finally be able to start programming after this chapter !
Which programs do we need ?
Depending on if we’re making static or dynamic sites we need different programs. In fact, when we’re creating dynamic sites we need a few more programs !
For a static site
Web designers that create static sites are lucky, in general they already have all the programs they need :
A text editor : in theory a program such as Notepad that’s pre-installed on Windows is enough, although it is recommended that you use a more advanced tool such as Notepad ++ or Dreamweaver. We’ll talk about the choice of editor at the end of this chapter.
A web browser : allows you to test your web pages. You can use for example Mozilla Firefox, Internet Explorer, Google Chrome, Opera, Safari, or any other browser you use for surfing the web. It is recommended to regularly test your sites on different browsers.
However, if you’re working on a dynamic site, these programs aren’t enough. It is necessary to install extra programs.
For a dynamic site
So that your computer can read PHP, it needs to act like a server. Don’t worry, you don’t need to buy a special machine for this : you simply need to install the same programs that are installed on the servers that deliver content on the internet.
What are these programs that we need ?
Apache : is what we call a web server. This is the most important program because it delivers the pages to the clients. However, Apache can only handle static web pages (it can only read HTML code). It therefore needs other programs to work with it.
PHP : is an Apache plugin that allows it to handle dynamic web pages in PHP. By combining Apache and PHP your computer will already be able to read PHP web pages.
MySQL : is the database management system I talked about in the introduction. It saves the data in an organised manner (such as the list of members on your site). We won’t need it immediately, but we’ll install straight away.
All these programs that will help us create dynamic sites are free. There are others (that sometimes cost money), but the Apache + PHP + MySQL combination is the most popular on web servers. It’s so popular that “packs” have been created which contain all these programs. It is possible to install them one by one but that takes time and won’t have any benefits (unless you’re a server administartor, which you probably won’t be ).
We’ll see how to install the “pack” that suits you depending on your operating system during the rest of this chapter.
In Windows : XAMPP
There are several packages that exist for Windows. I’m going to show you XAMPP which I have used for the last couple of years.
Start by downloading XAMPP from the official site. You don’t need to download the add-ons. The installer should be enough.
Once you’ve downloaded it just install it by leaving all the options by default (the different things don’t need to be services). It should install itself in a folder like C:/xampp and create a shortcut in the start menu.
When you launch XAMPP, an icon will appear in the task bar :
Clicking once on the icon will make the control panel appear. Just start Apache and MySQL to be able to run PHP :
You can now go to XAMPP’s home page. To do this simply go into XAMPP’s control panel and click on the Admin button next to Apache :
A similar screen to the one below should appear in your default browser. If the page displays itself, it means apache is running correctly.
The web page you can see on the screen has been sent your own local server that was installed at the same time as XAMPP. You are the only person that can see this server. Notice that the URL at the top of your screen starts with http://localhost which means that the work is on your computer.
To create a new web site you need to make a new folder inside C:/xampp/htdocs/. This is the folder for all web sites running on your localhost. For example, in the image below, I’ve made a new website called test :
Now, to view your new site, you simply need to open your browser and go to http://localhost/test (replace test with the name of your site) :
If you’re using Mac there is a good tutorial here
If you’re using Linux there is a good tutorial here
Use a good text editor
As you already know, to modify the source code of a web page there are several solutions :
- Use a simple text editor that you already have, such as Notepad. To open it go to Start/Programs/Accessories/Notepad. This program is enough to write HTML & CSS and even PHP but…
- The best is to use a specialised program that colours your code to make it easier to read. You’ve probably already installed one if you’ve read HTML – Getting Started from our HTML & CSS Lessons
Whatever editor you choose to use, it won’t change how you learn PHP: everything will be the same for everyone.