A “Hello World” program in PHP can serve as a reliable tool to check the installation and configuration of a development environment. When developers face challenges during the setup process, they can easily confirm if everything is functioning properly by running a simple program. So, troubleshooting time can be minimized, and frustration can be prevented.
In this article, I will guide you through the process of writing and running your first PHP program, both in the web browser and on the command line interface.
Display ‘Hello World’ on Web Browser
One of the benefits of using PHP to create web applications is that it can be easily integrated into HTML and displayed directly in the web browser.
So, this means that we can create a “Hello World” program in PHP and see the output in our browser without needing any additional tools or software. Now, let’s explore the process of achieving this.
In order to generate a PHP program that displays a “Hello World” message in the web browser. We need to create an HTML file and embed PHP code within it. We can do this by creating a file with a “.php” extension and using the opening and closing PHP tags. And that to indicate where our PHP code will be placed. Consider the following example for illustration:
<!DOCTYPE html> <html> <head> <title>PHP Hello World</title> </head> <body> <?php echo "Hello World!"; ?> </body> </html>
So, we have created an HTML file with a basic structure. We have included a title for the page in the head section, and a body section where we will display our “Hello World” message. Within the body section, we have embedded PHP code using the opening and closing PHP tags. Within the PHP code, we employ the echo statement to display the message “Hello World!” to the screen.
In order to view this “Hello World” program in a web browser, it is necessary to save the file with a “.php” extension and upload it to a PHP-supported web server. Once the file is uploaded, we can navigate to the URL of the file in our browser and see the output.
Before viewing the “Hello World” output on your web browser, make sure that the PHP server is running on your machine. If you’ve already installed PHP, you can access the output by navigating to
localhost/hello on your web browser. The anticipated output should take the form of the following:
Display ‘Hello World’ on Terminal or Command Line Interface
In another hand, PHP is not just for the web browser. We can also use it to create command-line scripts and tools. In this section, we will show you how to display “Hello World” on the terminal or command line interface (CLI).
To begin, launch a text editor such as Notepad or Sublime Text and initiate a new file. We will save this file with a “.php” extension to indicate that it contains PHP code.
Subsequently, we will proceed with writing the code that showcases the message “Hello World” on the command line. The most commonly employed method for accomplishing this is by utilizing the echo statement.
<?php echo "Hello World !"; ?>
After ensuring that you are in the appropriate directory, enter the following command:
This will execute the PHP script and display “Hello World” on the terminal or on the CLI.
In conclusion, the “Hello World” program in PHP is a simple yet powerful tool for testing development environments. And starting with PHP programming.
It’s easy to create in a few steps, whether in a web browser or the command line interface. By mastering this basic program, developers can build more complex web applications and gain a solid foundation in PHP programming.