PHP tutorial: Hello world

This tutorial helps you get started with PHP. It explains what you need to write your first PHP program. It starts with the basics: you will learn where you can find and download the tools needed, how to install these tools and it will take you all the way to building and runing your code. If you have never written a PHP program before this is the place to start. We have created this article, so you can get started with PHP, and move on to our sms examples. If you are already familiar with PHP, you can jump directly to one of the following SMS projects.

PHP sms examples:

PHP send sms with the HTTP rest api (code sample)
PHP send multiple sms with the HTTP rest api (code sample)
PHP schedule sms with the HTTP rest api (code sample)
PHP receive sms with the HTTP rest api (code sample)
PHP delete sms with the HTTP rest api (code sample)
Download the latest PHP sms api library from Github

What is PHP

PHP is a server side programming language. It is similar to a natural language, like English. It is used to talk to a computer. The major difference between a natural language and a programming language is that, programming languages have a more rigorous structure, to help the computer understand it better.

What is WampServer

WampServer refers to a solution stack for the Microsoft Windows operating system consisting of the Apache web server, OpenSSL for SSL support,MySQL database and PHP programming language. We will use it to run the PHP code locally on our computer.

What is a PHP Hello world program

A hello world program is the most simple program you can write. It simply prints out the sententce: Hello world on the computer screen. The Hello World program is the first program developers write in any programming language.

How to write your first program in PHP

To write your first program in PHP:

  1. Check prerequisites
  2. Setup WampServer
  3. Clear the original index.php file
  4. Write Hello World program in PHP
  5. Use echo("Hello world!") for printing
  6. Run your PHP Hello World program
  7. Check the output in the browser
  8. Fix errors if needed

Prerequisites

Here is what you need to get started. To write your first computer program in PHP, you need a Windows computer, WampServer and the example code presented below.

  1. Windows 10 computer
  2. WampServer
  3. Ozeki Hello World example project

Download WampServer

WampServer

To get started you have to visit the following link: https://wampserver.com and download the WampServer installer (Video 1).

Video 1 - Download WampServer

Download WampServer

Start by going to https://wampserver.com in your browser. Click on the Download button on the top to be automatically scrolled down to the downloads section. Click on WAMPSERVER 64 BITS (X64) to download the WampServer installer to your computer (Figure 1).

Figure 1 - Download WampServer

Install WAMP

The next step after downloading WampServer is to install it. This video show you how you can successfully complete the setup. When you've installed WampServer, it will automaticaly start to run (Video 2). After the installation process finished, we can go on creating our first 'Hello world!' PHP program.

Video 2 - Install WampServer

Write your 'hello world' program in PHP

In this part we are going to write our first PHP code using only the WampServer and a usual Windows nodepad.

First of all we should create a new file, called index.php in the previously opened /www/ folder.
Notice: It is helpful if you enable the system to show the file extensions, I'll show you how to do that in the upcoming video. The naming is not optional, because the local server will automatically open the file that is named index.

The www folder

Next we need to open the www folder. This is located inside the wamp64 folder, which itself is inside the directory we install WAMP into. In the www folder, we need to either edit the existing index.php file, or delete it and create a new one (Figure 2). This file is where we are going to write the PHP code and run in in our browser.

Figure 2 - The www folder

<?php
	echo("Hello world!");
?>
	

Code 1 - The 'Hello world!' program in PHP

"Hello world!" code

As you can see in the (Code 1) example, every PHP code starts with an opening PHP tag <?php, and ends with a closing PHP tag ?>. Inside these tags we use the echo command to display our message, "Hello World!".

Writing the "Hello world!" program in PHP

This video tutorial shows you how you can write the "Hello World!" code in PHP language. This is done by modifying the index.php file in Notepad (Video 3). The code itself uses an opening and a closing tag. Inside these tags goes the echo command. The argument of the echo command is the actual message that gets displayed on the page.

Video 3 - Writing the 'Hello world!' program in PHP

Running your 'hello world' program in the browser

To run your PHP code you should refresh the website and it will appear with the new content (Video 4).
Notice: It is important, that you have to save your PHP files into the www folder!
Path: C:\wamp64\www

Video 4 - Running the 'Hello world!' program in the browser

Output of the code

After you wrote your code you may now open it in your browser by typing http://localhost/ into your URL bar (Figure 3). Upon opening it, you can see that it displays the "Hello World!" message. This indicates that your code runs correctly. Now let's see what happens if the syntax of your code is not correct.

Figure 3 - The output of the code

What happens if I make a mistake in PHP

If you make a mistake in your PHP code, if you run your code the complier will alert you about you mistakes.

In our example we will remove one of the quote marks from the echo("Hello world!"); line.

What is syntax error in PHP?

Syntax error means I don't understand. If you talk to somebody in English and he does not understand what you say, he will reply with "I don't understand". If you talk to a computer in PHP and the computer does not understand what you say, he will reply with "Syntax error".

In the code below we will create a mistake intentionally by not putting a semicolon after the Hello World line. You will see, how the computer reacts, how we fix the error, and how we run the computer program successfully.

How to handle a syntax error in PHP?

In the code below we will create a syntax error intentionally by not putting a semicolon after the Hello World line. You will see, how the computer reacts, how we fix the error, and how we run the computer program successfully.

Making and fixing an error

In this video we aregoing to make an error in our PHP code on purpose. This will not run the code, therefore not display the message. An error message gets displayed instead (Video 5).

Video 5 - Making and fixing an error

Error message in PHP

When there is an invalid syntax in the PHP code, the code will fail to run. Here we can see that instead of "Hello World!", an error message appears (Figure 4). This message contains information about the type of error and where it occured. This is incredibly helpful for the developer, because it makes it easier to identify and fix mistakes in the code.

Figure 4 - Error message in PHP

Summary

This article explained how to get all the prerequisites for learning to code in PHP and how to write and run your first Hello World program. Now I recommend you to apply the Ozeki PHP SMS solutions for message organization, it offers proper, well-written method calls and a well-designed SMS class to allow you to configure the SMS parameters. Using PHP SMS API is great because it allows you to organize SMS messages the way you need and the Ozeki SMS Gateway provides excellent operating speed.

You may find out more information about this technology and the Ozeki SMS Gateway, so make sure that you continue reading. It is a good idea to check out the tutorial about how to send SMS from PHP.

Now your next thing to do is to download the Ozeki SMS Gateway and let the work begin!

More information