This tutorial covers the following topics:
- Creating PHP Scripts
- Testing PHP scripts on your laptop or computer
- What software do you need to write PHP?
Creating PHP Scripts
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
.php
extension), and instructs the PHP engine to process and run the script - The script runs, and when it’s finished it sends the 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 (a web server) executes the script when a request is made for the resource. For example, on a Linux os, the document root can be:
/usr/local/apache/htdocs/
Consider what happens when the script we discussed in the previous tutorial (Introducing PHP) is saved in the file:
/usr/local/apache/htdocs/example.php
When the Apache web server is configured with the PHP, it executes the script when requests to the URL http://localhost/example.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 run PHP scripts on your local computer, you need to install the following software:
- PHP
- 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 a database server, such as MySQL, is 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 databases with any web browser:
- For Windows:
- https://www.easyphp.org
You can download the developer’s version of EasyPHP from the above link. After installation, the files will be located in your program files directory under the EasyPHP directory. - http://www.wampserver.com
- https://www.easyphp.org
- For Linux, Windows, and macOS:
- https://www.apachefriends.org
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 tohttp://localhost
to make sure that the server is online. It should display the index.php file, which by default is located underC:\xampp\htdocs\index.php
on a Windows machine.
- https://www.apachefriends.org
- For macOS:
The document root
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:
- XAMPP:
C:\xampp\htdocs
- WampServer:
C:\wamp\www
- EasyPHP:
C:\...\eds-www
- IIS:
C:\inetpub\wwwroot
- MAMP:
Macintosh HD:Applications:MAMP:htdocs
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:
- Syntax highlighting:
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. - Auto-complete features:
This feature adds available PHP keywords to a drop-down list, allowing you to select the intended keyword from the list quickly and easily. - Code folding:
This feature lets you collapse snippets of code, making your workspace clutter-free and your code easy to navigate. - Auto-indent:
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. NetBeans
and Eclipse PDT
are both excellent and free IDEs:
- Eclipse PHP Development Tools
https://www.eclipse.org/pdt/ - Apache NetBeans IDE
https://netbeans.org/features/php/
Getting Started with PHP: