PHP Tutorial – Introduction

PHP is undoubtedly one of the most popular programming languages in use today. Most rankings of the top languages place PHP in the top five. In the category of “server-side programming language for websites,” PHP is far and away the leader. The popularity of PHP is due to a number of factors:

  • The language is relatively easy to learn but has advanced object-oriented features.
  • The code is easily embedded in HTML to create dynamic Web sites.
  • PHP is a component of the popular open source LAMP architecture.
  • It’s used by many of the most popular open source products such as WordPress, Drupal, and Joomla.
  • The interpreter is available on most hosting services so clients can develop their own software.
  • PHP is well documented and supported at
  • It is easily installed on the most popular platforms such as Windows, Linux, and Mac OS X.

This is the first in a series of articles that will teach you how to program in PHP. I intend to cover the core language including variables, constants, data types, operators, strings, arrays, branching, looping, functions, objects, files, directories, web development, and MySQL access. I’ll also provide overviews of many of the popular extensions. Along the way, I’ll build out the site’s PHP Quick Reference Guide.

If you want to create a training environment to follow along, any platform with Apache HTTP Server, MySQL, and PHP will suffice. I’ll be using the Ubuntu environment described in Saucy Salamander Cookbook for this series of articles. As of the time this article was written, you will end up with the following component versions:

  • Oracle VirtualBox 4.3.4
  • Ubuntu Desktop 13.10
  • Apache HTTP Server 2.4.6
  • PHP 5.5.3
  • MySQL 5.5.34
  • phpMyAdmin 4.0.6
  • MySQL Workbench 5.2.47
  • OpenJDK 1.7.0
  • NetBeans IDE 7.1

Execution Environment

PHP is an interpreted language. The PHP interpreter is a program that reads and executes source code from files. You can interact with a running program either from a command-line environment, such as a shell running within a Terminal window, or via a web browser when PHP code is embedded in a web page. Let’s take a look at both environments.

First Script

First, we need a PHP program to run. Let’s create a simple “Hello, world!” program. PHP scripts are composed of plain text so you can use your favorite editor such as vi, emacs, or gedit to create the source file. However, if you’re following along from the article I referenced above, the NetBeans IDE will be available for you to use. NetBeans provides a very productive environment in which to develop software using several different languages, including PHP. I’ll walk you through the process of creating a NetBeans project and then creating a simple PHP file within it. However, feel free to use any editor with which you are familiar.

After launching NetBeans you will see the startup screen as shown below.

NetBeans IDE

The NetBeans IDE provides a very productive environment for developing PHP software.

NetBeans organizes source files into Projects. Create a new project by clicking File→
New Project…
from the menu. You will see the New Project wizard as shown below. Select PHP from the Categories list and PHP Application from the Projects list as shown. Click Next.

Create PHP Project

Use the New Project wizard to create a new PHP project.

You will now see the Name and Location step of the wizard. Enter PhpTutorial as the Project Name. In the Sources Folder, select the path [home]/NetBeansProjects/PhpTutorial from the drop-down list, where [home] is your home directory. For example, my path is /home/charlie/NetBeansProjects/PhpTutorial. Leave the PHP Version at PHP 5.4, the Default Encoding of UTF-8 and click Next as shown below.

Project name and location

Give your PHP project a name and location for the source files.

You will next see the Run Configuration step of the wizard. Here, change the Run As value to Script (run in command line). Leave the PHP Interpreter at its default value. Click Next.

Run configuration

Create the initial run configuration to execute programs as command-line scripts. We’ll create a Web run configuration shortly.

You will now see the PHP Frameworks step of the wizard. Leave all of the frameworks unchecked and click Finish as shown below.

PHP Framework selection

We don’t need a PHP framework at this time so leave all of the boxes unchecked.

You should now see your PhpTutorial project in the Projects pane of the Navigator on the left side of the IDE. NetBeans creates a new PHP file called index.php for you and opens it in an editor tab. Notice that the PHP script opening tag <?php is already entered into the file for you.

The index.php file is created and opened in an editor

The New PHP Project wizard creates an index.php file for you and opens it in an editor tab.

Replace all of the text in the index.php file with the script below. This program simply uses the PHP echo statement to display the text Hello, world! The “\n” character at the end of the string is an escape sequence that represents the newline character.

echo "Hello, world!\n";

Now, we’ll run the script within the IDE. Click the green Run Project button in the toolbar. You should see output from the script in the Output pane at the bottom of the IDE as shown below.

Hello, World! script

A Hello, World! script is required when learning any new programming language.

Notice that the command-line interpreter (/usr/bin/php) was executed, passing the full path of your program ([Home]/NetBeansProjects/PhpTutorial/index.php) as a command-line parameter. You could execute the same command from a Terminal window as well as shown below.

Command-line script run in Terminal

The same command-line script can be run in a Terminal.

Web Server Version

We’ll now run the same program using the Apache HTTP Server and view it with a web browser. We need to first create a new Run Configuration. In the IDE, select Run→Set Project Configuration→Customize… from the menu. You will see the default run configuration that was set up at the time the project was created as shown below. This run configuration is set up to run the PHP program as a command-line script with the output displayed in the Output pane of the IDE as we saw earlier. Click the New… button beside the configuration name to create a new run configuration.

Additional run configuration for web deployment

You can create additional run configurations for different purposes. We will now create a configuration that will deploy the project to a local web server for execution.

You will now see the Create New Configuration dialog as shown below. Enter Web as the Configuration Name and click OK.

Run configuration name

Give the new Run Configuration a name.

Back on the Run Configuration dialog, change the Run As option to Local Web Site (running on local web server) as shown below. This configuration will run the PHP script on the locally-installed Apache web server.

Run configuration target

Direct the run configuration to deploy the application to a local Web server.

Now, click the Sources category on the left side of the dialog. Enable the checkbox next to Copy files from Sources Folder to another location. In the Copy to Folder box select /var/www/PhpTutorial if it is not currently selected. This option determines where the PHP scripts will be deployed; in this case to a subdirectory of Apache’s document root. Click OK.

Run configuration copy to folder option

Tell NetBeans to copy the project files to the local web server’s document root.

You are returned to the IDE. If you now click the green Run Project button in the toolbar, NetBeans will launch your browser and navigate to the URL http://localhost/PhpTutorial/index.php as shown below.

Hello, World! in a browser

The Hello, World! application running on the web server.

Open a Terminal window and take a look at the document root for the Apache HTTP Server, /var/www. Notice that NetBeans created a PhpTutorial subdirectory and copied the project’s files into it. This automatic deployment happens whenever you make a change to the project such as adding a new file, editing a file, and creating a subdirectory. Looking inside the /var/www/PhpTutorial directory you will find the index.php file you just created. Because the file is stored in the document root hierarchy and it has a .php extension, Apache recognizes it as a PHP source file and executes the code within. The output is then displayed by the browser.

If you view the source of the resulting web page (right-click in the browser window and choose View Page Source from the menu) you will see that only the text Hello, world! appears. Although Firefox is willing to display a page with only text, a proper web page would include some HTML markup as well. Let’s create a new file called hello.php. Select File→New File... from the menu. You will see the Choose File Type step of the New File wizard as shown below. Ensure that the Project field is set to PhpTutorial. Select PHP from the Categories list and PHP Web Page from the File Types list. Click Next.

Create a new PHP Web Page file

Create a new PHP Web Page file in the project.

You will now see the Name and Location step of the wizard. Enter hello as the File Name. Note that the file will be created in the project’s directory within your home directory. Click Finish as shown below.

Name and Location dialog

Give the file a name.

The hello.php file is created and opened in an editor tab. Since you chose PHP Web Page as the file type, NetBeans added some boilerplate HTML markup for you and provided a place to insert your code. Delete the HTML comment at the top of the generated file. Enter Hello, world! between the <title> and </title> tags. Replace the code within the <body> and </body> tags with the echo statement as shown below.

<!DOCTYPE html>
        <meta charset="UTF-8">
        <title>Hello, world!</title>
        <h1><?php echo "Hello, world!"; ?></h1>
PHP embedded in HTML

Embed a PHP echo statement within the page’s HTML.

This PHP Web Page intermixes standard HTML with PHP code. The HTML parts are just passed through to the browser untouched. However, when the <?php tag on line 8 is reached, the interpreter begins parsing and executing the PHP code until the next ?> tag is reached. The remaining HTML code is then passed unchanged to the browser. Execute the file by right-clicking within the editor window and selecting Run File from the context menu. You should now see the new web page as shown below. Notice that the browser window and tab display the page title and that the Hello, world! text is displayed as an <h1> heading.

Output displayed in browser

The output is displayed in the browser.

View the page source as you did earlier. Note that the output of the PHP echo statement is embedded directly into the page source.

PHP output embedded in HTML

The page source shows that the echo statement was executed by the PHP interpreter and its output embedded within the HTML.

Multiple Run Configurations

A PHP page can include any number of embedded <?php ... ?> tag pairs intermixed with HTML. Code within the tags is parsed and executed by the PHP interpreter, while everything else is passed straight to the browser. You might be wondering what we would see if we executed this version using the command-line interpreter. You can switch back to the command-line run configuration by selecting Run→Set Project Configuration→<default> from the IDE’s main menu (or by using the drop-down box in the toolbar). Run the file again by right-clicking the editor window and selecting Run File from the menu. Notice that the output is now displayed in the Output pane within the IDE.

Output shown in Output Pane

By changing the Run Configuration you can redirect the output back to the Output Pane.

During the tutorial I’ll intermix examples from both the command-line and Web versions of the PHP interpreter. While learning the basics of the language it’s often easier to experiment using the command-line interface so that the HTML markup doesn’t obscure the language elements under discussion. However, PHP is first-and-foremost a scripting language used for Web applications. When we get to the Web-specific parts of the tutorial, I’ll show most examples using the web server and browser.

In this article I walked you through the creation of a simple Hello, world! PHP script in NetBeans. We created a project and a run configuration for command-line script execution and viewed the program’s output within the IDE. We then created another run configuration that delploys the project to the local Apache Web Server’s document root hierarchy and launches a browser with the appropriate URL to execute the script. We were then able to view the program’s output in the web browser. Finally, we created a new PHP Web Page with some HTML markup and a line of embedded PHP code. Using our two run configurations we were able to view the output in both the browser and in the IDE’s Output pane.

In the next article we’ll begin the tutorial with a look at some basic PHP syntax.

Speak Your Mind