- This tutorial covers the following topics:
- Testing PHP scripts on your laptop or computer
- What software do you need to write PHP?
The process of running a PHP script on a Web server looks like this:
- A visitor requests a Web page
- The Web server recognizes that the requested URL is a PHP script (with a
.phpextension), and instructs the PHP engine to process and run the script
- The script runs, and when it’s finished it sends response to the Web browser, which the visitor then sees on their screen.
If you save a PHP script in a file with a
.php extension under the directory configured as Apache's
document root, Apache executes the script when a request is made for the resource. For example, on a Linux os, the document root can be:
Consider what happens when the script shown in Example 1 (described in previous page Introducing PHP 7) is saved in the file:
When Apache web server configured with the PHP, it executes the script when requests to the URL
http://localhost/example-1.php are made, assuming the web browser is running on the same machine as the web server.
Testing PHP scripts on your laptop or computer
To test PHP scripts on your local computer, you need to install the following software:
- Web server – Apache HTTP (or Nginx, IIS, lighttpd etc.)
- Database – MySQL or MariaDB (and phpMyAdmin to manage databases and their tables)
One of the biggest hurdles for new programmers is starting. Before you can write your first line of PHP, you must download Apache and PHP, and MySQL (or MariaDB), and then fight through installation instructions that are full of technical jargon you might not understand yet. This experience can leave many developers feeling unsure of themselves, doubting whether they’ve installed the required software correctly.
Fortunately, there are several options that take all the pain out of setting up your development environment, whether you create applications for Windows, Mac, or Linux machines. These options include all-in-one solutions for setting up Apache, MySQL, and PHP installations. The following topic introduces some of the available options.
Installing PHP, Apache and MySQL Stack
To view PHP code in a browser, the code first has to be parsed on a web server, such as Apache, with the PHP module installed. To create dynamic web pages the database server, such as MySQL, also required . There are numerous pre-configured development packages available that provide a combination of PHP, MySQL/MariaDB database and Apache web server products, along with other tools such as PHPMyAdmin (to manage MySQL/MariaDB databases through PHP):
- For Windows:
- For Linux, Windows and macOS:
The XAMPP package is free and is pre-configured so that the components will talk to each other. XAMPP is more popular because it has Windows, Linux, and macOS versions. After installing the web server, point your browser to http://localhost to make sure that the server is online. It should display the index.php file, which by default is located under C:\xampp\htdocs\index.php on a Windows machine.
- For macOS:
Document root - where to Locate PHP Files
You need to create your files in a location where the web server can process them. Normally, this means that the files should be in the server’s document root or in a subfolder of the document root. The document root is the folder (a directory) where the website files for a domain name are stored.
The default location of the document root for the most common setups is as follows:
To view a PHP page, you need to load it in a browser using a URL. The URL for the web server’s document root in your local testing environment is
http://localhost. You can also use
http://127.0.0.1 which is the loopback IP address all computers use to refer to the local machine.
What software do you need to write PHP?
You don’t need any special software to write PHP scripts. PHP code is plain text and can be created in any text editor, such as Notepad on Windows, TextEdit on Mac OS or vi, nedit, emacs, or pico on Linux based os.
Most experienced PHP developers use an IDE (integrated development environment). An IDE is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of a source code editor, build automation tools, intelligent code completion, and a debugger.
Many beginners have some difficulties using an IDE because IDEs have so many features that beginners are simply left confused. There are some features you should expect from a good editor:
To make your code much easier for reading.
Built-in function references:
When you enter the name of a function this feature displays available parameters and a short description of what the function does.
This feature adds available PHP keywords to a drop-down list, allowing you to select the intended keyword from the list quickly and easily.
This feature lets you collapse snippets of code, making your workspace clutter-free and your code easy to navigate.
This automatically indents the code you write in a consistent manner.
Free PHP Code Editors
Beginners may find it easier to start with a free and simpler code editor. I really like
NotePad++, it is as easy to use as any editor, and it provides most of the features just listed out of the box, including excellent auto-complete for PHP and many other languages:
Free PHP IDEs
If you want to go with an IDE there are many good IDEs to choose from.
Eclipse PDT are both excellent and free IDEs: