Hands-on with PHP — Development Environment

< Home < PHP IntroductionPHP Syntax >

What’s a Software Development Environment

You have learned in the Introduction lesson that, as a server-side scripting language for web development, PHP scripts are executed on the server. So, at the very least, you will need to install a PHP-enabled web server on your development computer so that you can test your PHP scripts during development. In addition, you also need a software tool to write your PHP scripts on the computer. In other words, you need to set up a software development environment for PHP on your development computer.

A software development environment is the location where you write and test your software applications before uploading them to the staging environment and then to the production environment. The most common location for your development environment is your own computer. For the purpose of testing your PHP scripts, your target software development environment should at least comprise these three software components:

  • Web Server — Apache.
  • Code Interpreter — PHP engine.
  • Code Editor — any text editor of your choice.

What’s Web Server

A web server is a piece of computer software installed on a computer that hosts a website, which is a collection of web resources which may include HTML files, server-side script files, CSS files, JavaScript files, image files, and so on. Typically, you request a web resource file, e.g. index.php, from a web server by entering into the address bar of a browser an appropriate URL that points to that resource file on that web server, e.g. www.mywebsite.com/index.php. Upon receiving your request, the web server locates the resource file, call upon the appropriate server-side scripting engine to parse and execute any server-side scripts contained in that file, the results of which together with any HTML markup are then returned to your browser for rendering as a HTML formatted web page. Based on the HTML markup, your browser might then make further requests for other resources e.g. CSS, JavaScript, and image files.

There are different types of web server software available that supports PHP — Apache, IIS, Nginx, Lighttpd, etc. Among them, Apache is the most widely used web server software for PHP website.

Apache as Web Server

For the purpose of learning the PHP lanugage, it really doesn’t matter which web server software you use as long as it supports PHP. For practical reasons, however, you should use the web server software that your web host uses. For PHP websites, more often than not, your host is using Apache as the web server. Apache is the fruit of the open source Apache HTTP Server Project undertaken by the Apache Software Foundation with supports from volunteers all over the world. Apache’s popularity as web server software can be attributed to the facts that:

  • Apache has a long proven track record of reliability and performance.

  • Apache is open source thus it has no licensing fees or costs to bear.

  • Apache can be installed on most OS platforms, e.g. Linux, Mac, Windows.

  • Apache has been constantly maintained, upgraded, and supported by a network of volunteers.

  • There are plenty of documentation and helps available online.

  • Apache is one of the most feature-rich web servers available.

Setting up Apache and PHP

The easier and faster approach is to install them from some all-in-one software packages. One of the most popular all-in-one packages is this XAMPP available free for download and installation from Apache Friends. XAMPP stands for Xross-platform, Apache, MariaDB (a community-developed fork of the famous MySQL), PHP and Perl. Installing Apache and PHP using XAMPP is just a matter of a few mouse clicks away. Read the installation instructions and FAQs available at Apache Friends and proceed to install the XAMPP on your development computer.

By default, XAMPP for Windows is installed in C:\xampp directory while that for Mac is installed in /Applications/XAMPP directory. The default document root directories where web resources are kept are then C:\xampp\htdocs in Windows and /Applications/XAMPP/xamppfiles/htdocs in Mac respectively.

XAMPP comes with a control panel where you can start and stop servers easily. Start the Apache web server and keep it running, then proceed to set up a website.

Setting up a Website

A website is actually a folder that contains all web resources and is kept in the document root directory of a web server.

To set up a website called myfirstwebsite, for example, first create a sub-directory called myfirstwebsite inside the htdocs directory; then inside the myfirstwebsite directory, create a PHP file called index.php that contains the following PHP script using a text editor:

<?php
echo "<h1>My first PHP page from my first website</h1>";
?>

You have just set up a website called myfirstwebsite containing one resource file called index.php on the Apache web server. To visit the index.php, enter this URL http://localhost/myfirstwebsite/index.php into the address bar of your browser and then hit the Enter key — do you see something that looks like the screenshot in Figure 1?

Figure 1: Your First Website

Figure 1: Your First Website

PHP Configuration

Every PHP installation comes with default configuration settings contained in a configuration file called php.ini. The php.ini is read whenever PHP starts up and it contains a list of configuration directives whose values can be changed to affect the functionality and behaviour of PHP such as the default directories to look for files when a file path is not given, the maximum number of files allowed to be uploaded simultaneously, and the maximum file size permissible for each file upload, and so on.

You can view a complete list of the configuration settings in the php.ini file on a browser by calling the phpinfo() function. In the myfirstwebsite above, create a new PHP file called phpinfo.php that contains the following PHP script using a text editor:

<?php
  phpinfo();
?>

Enter this URL http://localhost/myfirstwebsite/phpinfo.php into the address bar of your browser. Do you see this?

Figure 2: Output of `phpinfo()

Figure 2: Output of `phpinfo()

The php.ini file is well commented. Before you make any changes to it, be sure to keep an original backup.

Going Forward…

Now that you have set up a PHP development environment on your computer, you can use it to check out exercises in subsequent lessons as you learn.

< Home < PHP Introduction

Tagged with:

Speak Your Mind