In this article, you will learn how to write the first PHP program to print Hello world. Firstly, to learn more about how PHP works, you have to read what is PHP program tutorial.
Table Of Contents
The PHP scripting language has an interpreter, that depends on four phases to compile the PHP source code. And that can be done using the Zend Engine interpreter.
The four phases, like the below steps.
- The lexical Analysis
- The Parser
- The Compilation
- The Execution
According to these four stages, you will see the final result or the output in the web browser or in the command line interface.
So to start your first PHP program, you have to install PHP server on your operating system.
Print PHP Hello World using Command Line Interface
The command line interface or CLI is a program that allows you to write your commands on the operating system to run another program in the OS.
Anyway, In this section, we are going to run our PHP program using the command line interface and that can be done using the PHP server software.
Once you configure the PHP server with your CLI, you can build a program in PHP using the CLI. Let’s build the “Hello World” program.
Based on your operating system, you have to open any directory and create a folder with “hello” and inside it create a file with the PHP extension and name it “index.php”.
Inside this file, copy, and paste the following PHP code.
<?php echo "Hello World !"; ?>
Once you save the changes, let’s run it using the command line interface.
Currently, my operating system is windows, so I will open the command line interface using the following steps.
- Click on the window icon, “start”.
- Search for the “command prompt”.
- Run it and navigate to the PHP project folder, “hello”. And that can be done using this command
cd /d C:\Users\dell\Desktop\hello.
In the final step, I have to run the PHP file using the following command.
It would print the “hello world” message like in the below image.
Note: if you are using windows and didn’t configure the PHP server with the Environment Variables on your operating system, you will never be able to run the PHP script in the CLI. And you will see the following error.
C:\Users\dell\Desktop\hello>php index.php 'php' is not recognized as an internal or external command, operable program or batch file.
But, if you already have another operating system such as macOS, or Linux you just need to have only the PHP-CLI and you can run it directly using the terminal.
On the other hand, you can build a program with the “Hello World !” message using your web browser. Let’s do that with your local machine.
Print PHP Hello World using Web Browser
Before getting started, you have to make sure that your PHP server is running. I am already working with the Xampp Server.
Firstly, you have to create the project folder in your server’s public root. In my OS, the public root for the Xampp Server is like the one below.
If you are already using the WAMP server, you will find it in the following path.
And then create a folder inside and name it with “hello”.
Inside this folder, create an “index.php” file and copy and paste the same code as below.
<!DOCTYPE> <html> <head><title>Hello World</title></head> <body> <?php echo "Hello World !"; ?> </body> </html>
In the following step, you have to navigate to the project name through the following root by the web browser.
You will see the hello message like the below image.
In this tutorial, you understood how to print the PHP hello world message using two methods. The command line interface and the web browser.