Write your first PHP application

In this tutorial we introduce you to writing and running a simple PHP application. We will learn some PHP syntax, how we can integrate HTML in a PHP file and how to run the application in a web browser.

Here's a table of contents of what you'll learn in this lesson:
(click on a link to skip to its section)

Let's jump right in.

Before we begin

Before you begin, you must have a PHP parser installed on your system.

If you don’t have an environment set up yet, please follow one of the links below before continuing.

Your first PHP application

First things first, if you haven’t started the web server yet, open the XAMPP Control and start the Apache web server module. Without it, we won’t be able to run our application.

Next, we need to create a file to hold the PHP code.

  1. Open your Atom IDE. In the Project Pane on the left, right-click on the PHPProjects folder and select New File.
  2. Name the file HelloWorld.php and press Enter or Return on your keyboard.

Write the code below into your HelloWorld.php file and save it by going to File > Save.

Code: first PHP app
<?php

// my first PHP app
echo "Hello World";

?>

In your web browser navigate to the url: localhost/PHPProjects/HelloWorld.phpOpens up in a new page

You should see the words “Hello World” on the page.

If you gave your project folder or php file a different name, please make sure that reflects in the url, otherwise the PHP script won’t run.

Breakdown of your first PHP application

In this breakdown, we will go through the code line by line so you can have a basic understanding of the syntax of a PHP application.

Open and close tags

The open and close tags in PHP indicate a dedicated space between them for PHP code. PHP cannot parse code outside of these tags.

Example: open and close tags
<?php

?>

The interpreter will scan the document, looking for these tags. When it finds them, it will attempt to execute the code within.

Other types of code, such as HTML, is allowed within the document. Because of this, we explicitly tell the interpreter where the PHP code is written.

If there were no open and close tags, PHP scripts that contain other types of code could become confusing quickly and the interpreter might misinterpret conflicting code and break the application.

Comments

PHP allows us to document our code, and one of the ways we do that is with comments. Comments are completely ignored by the interpreter at runtime.

Single line comments are prefixed with two forward slashes // . Anything on that same line, after the forward slashes, will be ignored by the interpreter.

Example: single line comments
<?php

// my first PHP application

?>

If you’re working in an IDE, it will usually recolor comments into a grey color to indicate that the code is a comment.

The echo command statement

The echo statement in PHP is a command statement that will display whatever comes after it on the page.

Example: echo command statement
<?php

// My first PHP application
echo

?>

In this particular case we print the words “Hello World”, known as a string, on the page. The contents of a string are always enclosed within quotes.

Example: string
<?php

// My first PHP application
echo "Hello World"

?>

At the end of the command we write a semicolon operator ; . A semicolon is a statement terminator and indicates to the interpreter that this command is ended at this point.

Example: semicolon statement terminator
<?php

// My first PHP application
echo "Hello World";

?>

Mixing PHP with HTML

As we’ve mentioned previously, PHP can be written alongside other languages, like HTML, in the same document.

When writing PHP in a document with HTML code, we must remember that the document must be a PHP type document with the .php extension.

If we write PHP code in a document with a .html extension, it will not be executed.

Quick HTML Intro

HTML is a markup language, it uses opening and closing tags to specify elements in a hierarchical fashion. Inside these elements we can specify text, links and images which will be shown on a webpage.

The following is an example of what an HTML tag pair looks like.

An open tag is written by using a keyword in between a less than < and a greater than > symbol.

A close tag is written the same as an open tag but has a forward slash in front of the keyword.

Example: html tag pair
 <title></title>

The most basic html document consists of <html> , <head> and <body> tags. The head and body tags are nested inside the html tags.

Clear the PHP code we wrote earlier from the HelloWorld.php document and copy & paste the HTML below into the file.

Example: barebones html
<!DOCTYPE html>
<html><head><title>Hello World</title></head>

<body>

    <h1></h1>

</body>
</html>
HTML & PHP

Now that we have some HTML to work with we’ll write our PHP code inside of it. We want to write the same echo statement, but this time inside the <h1> tag in the body of the HTML.

In between the <h1> open and close tags, we simply write the same PHP code that we did in the very first example, only this time we write it all on one line.

Example: inline php
<!DOCTYPE html>
<html><head><title>Hello World</title></head>

<body>

    <h1><?php echo "Hello World"; ?></h1>

</body>
</html>

When we refresh the webpage in the browser we can see that the text “Hello World” is now a heading, bigger and bold.

We have successfully written both a PHP only application and a PHP webpage.

Summary: Points to remember

  • PHP code will only be executed in a document with the .php extension.
  • PHP code will only be executed if it is wrapped in open and close PHP tags: <?php ?>
  • We can combine PHP with other languages like HTML by writing the HTML inside our .php document.