PHP “Hello World” is a simple and traditional introductory program that is often used to demonstrate the basic syntax and structure of a programming language.

Here’s a simple example of a “PHP Hello World” program:

// This is a PHP script
echo "Hello World!";

To run a PHP script, you typically need a server that supports PHP. Many web servers, such as Apache or Nginx, are capable of processing PHP code. You save the PHP script with a .php extension and access it through a web browser by navigating to the corresponding URL.

In the following section, you will learn how to do that through examples. But before we delve into that, let’s first take an overview about echo or print in PHP.

PHP Output Techniques

This section focuses on two primary methods, namely echo and print, which play pivotal roles in presenting content on web pages.

The echo construct is used for outputting data. It is not a function but a language construct, which means it doesn’t necessarily require parentheses, although they can still be used. One notable feature of echo is its ability to take multiple parameters, separated by commas. This allows for the output of multiple values in a single echo statement. Importantly, echo has no return value, making it unsuitable for use in expressions.

For example:

  echo "this is an output";

while print is a language construct in PHP but is treated as a function, and as such, it requires parentheses. Unlike echo, print can only take a single parameter, limiting it to outputting one value at a time. It returns a value of 1, making it suitable for use in expressions if needed.

For example:

  print("this is an output");

Let’s use them to display the ‘hello world’ text on the web browser.

Viewing the Output of “Hello World” in a 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. Consequently, we can effortlessly create a ‘Hello World’ program in PHP and observe the output in our browser without needing any additional tools or software. Now, let’s explore the process of achieving this.

To create a PHP program that displays a ‘Hello World’ message in a web browser, you need to generate an HTML file and embed PHP code within it. This involves creating a file with a ‘.php’ extension and using the opening and closing PHP tags to indicate where your PHP code will be placed. Consider the following example for illustration:

<!DOCTYPE html>
	<title>PHP Hello World</title>
		echo "Hello World!";

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.

If you don’t have an online server, simply set up a PHP server on your local machine.

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:

PHP Hello World Using the Web Browser

That’s all you need to do to view the ‘Hello World’ text in your browser. Now, let’s explore another method to execute ‘Hello World’ in the Command Line Interface (CLI).

Running the ‘Hello World’ Program in the 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.

   echo "Hello World !";

After ensuring that you are in the appropriate directory, enter the following command:

php hello.php

This will execute the PHP script and display “Hello World” on the terminal or on the CLI.

PHP Hello World Using the CLI

Let’s summarize it.

Wrapping Up

the “PHP Hello World” program serves as a fundamental and customary introduction to the syntax and structure of the PHP programming language. We explored the traditional PHP script, which involves using the echo statement to display the iconic “Hello World” message. Understanding PHP’s output techniques, particularly the roles of echo and print, provides a foundational insight into presenting content on web pages.

We then demonstrated how to effortlessly display the “Hello World” text in a web browser by integrating PHP into an HTML file. This process highlighted the seamless integration of PHP with HTML and showcased the simplicity of creating dynamic content for web applications.

The accessibility of PHP extends beyond web browsers, as we explored an alternative method to execute the “Hello World” program in the Command Line Interface (CLI). By saving a PHP script with a “.php” extension and utilizing the echo statement, we executed the program and witnessed the output directly in the CLI.