PHP Hello World Program

In this tutorial, we will learn how to write your first PHP “Hello World” program in the VS Code editor and run it with the help of XAMPP server.

Before we begin, you should have the following software installed on your computer system:

XAMPP: XAMPP is a free, open-source, and cross-platform web server solution stack package developed by the Apache Friends organization. It mainly includes the Apache HTTP Server, the MariaDB database engine, PHP and Perl server-side programming languages.

Additionally, XAMPP offers an email server, an FTP server, and a self-signed certificate to enable the Apache web server to run in HTTPS mode. Installation packages are available for Windows, macOS, and Linux, making it a versatile choice for developers across different platforms.

Visual Studio Code (VS Code): It is a free, popular, and open-source code editor developed by Microsoft for Windows, Mac OS, and Linux. It is easy to use as any editor.

VS code includes the support for debugging, syntax highlighting, intelligent code completion, snippets, code refactoring, etc. It supports almost every major programming language, including PHP. It also offers an extension marketplace that contains a rich library of extensions to add new features to power up your VS Code experience.

However, there are many good IDEs and editors to choose from. For example, NetBeans and Eclipse PDT are both excellent, and free IDEs. But I will use the VS code editor to write the first PHP hello world program, which is my favorite code editor. You can use any code editor as your choice.

If you did not install XAMPP server and VS code editor, you can go for this tutorial (Install PHP) where you will learn to download and install XAMPP server and VS code editor step by step as well as configuration.

Write PHP “Hello World” Program in VS Code


There are the following steps that you follow to write your first hello world program in PHP. They are as:

1. Open VS Code: Launch Visual Studio Code editor in your computer system.

2. Create Project Folder: If you have installed XAMPP server in your computer system, go to the XAMPP installation folder in your drive where you have installed it. When you will click on the XAMPP folder, you will get htdocs folder inside it.

The htdocs is the root directory or folder where you have to place all your PHP files inside this folder. A PHP file is a plain text file with .php extension that contains the PHP code. It can also contain HTML, CSS, JavaScript, along with the PHP code.

When a Apache web server executes a PHP file inside the htdocs, it processes the PHP code and generates dynamic HTML output, which it then sends to the client’s browser.

Now create a project folder named “PHP_Project” inside the htdocs directory. You’ll be making many PHP files soon to learn PHP programming, so it is a good idea to keep them organized.

3. Open the Project Folder: Open the VS code editor and click on File > Open Folder. Then, go to the XAMPP installation folder, open the htdocs folder, and select the PHP project folder you created inside htdocs. Once opened, you will see your PHP project displayed in the Explorer panel on the left side of VS Code.

4. Create a New PHP File: In VS Code editor, create a new file by clicking on File > New File and save this file as index.php inside the PHP_Project folder.


5. Write the PHP Code: In index.php file, type the following PHP code:

<?php
echo "Hello World!";
?>

In this code, we have used the echo statement to output or display “Hello World!” on the web page. The echo statement in PHP is used to output one or more strings on the web page. It is one of the most commonly used statements for displaying text, HTML content, or the result of PHP expressions.

The echo statement can display simple text, HTML elements, or any combination of PHP variables and static content. It sends this output to the web browser as part of the HTML page.

How to Run PHP Program Using XAMPP?


There are the following steps through which you can run the PHP hello world program in your favorite or default web browser. They are as follows:

1. Start Apache Server: Open the XAMPP Control Panel and click the “Start” button next to the Apache web server and MySQL. Once started, you can minimize the control panel.

2. Run PHP in Browser: Unlike other programming languages, PHP does not directly run with debugging in VS Code since it is server-side programming language. Therefore, you open your favorite web browser and navigate to

http://localhost/your_project_folder/index.php

Here, your_project_folder is the name of your PHP project in the htdocs directory.

Since the name of PHP project is PHP_Project, the URL will be

http://localhost/PHP_Project/index.php

This will display a message “Hello World!” as output in the browser. In PHP development, the browser acts as the interface to see the output of your PHP code, in which Apache server processes the PHP files.

Another Method to Run PHP Code


If you want to run the PHP code directly from the VS code editor, you will have to install two extensions in your VS code. They are:

  • All-in-One PHP support
  • Live Server

For this, click on the extension icon and type All-in-one PHP support and install it. After installing, type Live server and install it.

Run PHP Code:

Now click on the “Run” in VS code editor and then click on “Run with debugging” option. Then, you will get two options:

  • Run current Script in Console
  • Launch built-in server

If you choose the first option, it will display the output on the debug condole. On choosing the second option, it will launch your default web browser and display the output.

In case you get an error like PHP executable path not setting, then you will have to configure the correct PHP executable path in VS code. For this, go to the previous tutorial (Install PHP) and configure it step by step.