PHP is amongst the most widely used programming languages for web development. Since it is an open-source, server-side scripting language with relatively simple architecture, compared to other MVC-based frameworks, it has become a very common language.
A “Hello World” program, as the first program, has become a culture whenever learning a new programming language. Hence, following the footsteps, you will learn how to create a ‘Hello World’ program in PHP to mark the beginning of your journey in PHP.
Let’s get started.
Prerequisites for PHP Development
You need a certain environment to write PHP scripts. Since PHP is a server-side language, it requires a web server for the script to run. In this article, you will create a local webserver.
For PHP development, you need:
- XAMPP Software.
- Code editor (e.g., Visual Studio Code)
How to Setup XAMPP Software?
XAMPP is an open source software which stands for:
X = cross platform
A = Apache
M = MariaDB
P = PHP
P = Perl
It is a complete web solutions package. You will be mainly using it for creating a local web server for this tutorial. XAMPP is also used to set up a MySQL database, which you will learn in the later sections.
To set up XAMPP:
1. Download and install XAMPP from the official website.
2. After installation, open the XAMPP control panel and start the Apache webserver.
Now, to write the code, you need a code editor.
How to Install Code Editor?
You will use Visual Studio Code as the code editor. You may also download any code editor of your choice. Eg: Atom, Notepad++ etc.
Download the Visual Studio Code from the official website.
Now, have a look at the syntax for writing PHP scripts.
A PHP script is very similar to how HTML codes are written. It starts with <?php and ends with ?> while the code goes in between.
For every PHP file, the default extension code is ".php".
A PHP file usually contains HTML codes with PHP scripts embedded into them.
Write and Run a “Hello World” Program
Now that you have set up the environment for PHP and learned its syntax let’s move onto creating the Hello World program.
Follow this step-by-step to create your PHP program:
1. Open the directory where XAMPP is installed. By default, XAMPP is installed in the C:\ drive.
2. Open the XAMPP folder and locate a folder named “htdocs” in it.
3. Inside that “htdocs” folder, create a folder and name it anything suiting your project.
Here, it has been named as “demo”.
4. Now open Visual Studio Code and click on “open folder”.
5. Locate to C:\xampp\htdocs and select the “demo” folder you created.
6. Create a file named “index.php” inside the demo folder.
7. Since PHP is embedded into HTML codes, write the following HTML code along with the PHP script.
In the above code, “echo” is used to print the string in front of it.
8. Save the file and open your web browser and type “localhost” (Since our server is locally hosted) followed by the folder name that you created and hit enter.
9. As you can see, you have got the output for your first PHP program.
Are you a web developer or interested in building a website? Enroll for the Full Stack Web Developer - MEAN Stack Master's Program. Explore the course preview!
This brings us to the conclusion of ‘Hello World’ in PHP article. You have learned how to set up the complete environment for PHP development and how to write and execute your first program in PHP.
Check our next tutorial on the PHP Login form.
Do you have any questions regarding the article? Please mention it in the comment section of the "Hello World in PHP" article, and we'll have our experts answer it for you. Happy learning!