Table of Content
You always start with “Hello World” when you learn a new language. It keeps things simple. You see right away how PHP works with this example.
You need PHP installed and a server like Apache or Nginx. Or you can use the PHP built-in server.
PHP Hello World Code Example
Start with a basic text editor like Notepad or VS Code. Save your file with a .php extension—for example, hello.php.
You only need two lines to write your first PHP program. Here is the full code:
echo "Hello World!";echo prints text to the screen. It prints Hello World! in this case.
So how do you run it in the Browser?
- Put your
hello.phpfile in your web server’s root folder (likehtdocsin XAMPP orwwwin WAMP). - Open your browser and go to
http://localhost/hello.php.
You will see the text Hello World! on the screen.
Run PHP Hello World in the Command Line with Example
- Open your terminal.
- Go to the folder where your
hello.phpfile is. - Run this command:
php hello.phpThe terminal will print: Hello World!
PHP Hello World Program Embedded in HTM
You can mix PHP with HTML. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Hello PHP</title>
</head>
<body>
<h1>
<?php echo "Hello World!"; ?>
</h1>
</body>
</html>You will see the headline with the text “Hello World!” when you open this in a browser
Wrapping Up
You learned how to write and run your first PHP program. You saw how to use echo and where to place your file.
Here is a quick recap:
- Use
<?phpto start PHP code. - Use
echoto print output. - Save the file with a
.phpextension. - You can run it in a browser or terminal.
- You can also embed it in HTML.
FAQs
Do I need to install a server to run PHP?
Yes, you need a web server like Apache or Nginx. You can also use PHP’s built-in server.
Can I run PHP without a browser?
Yes, you can run PHP in the command line with php filename.php.
What is the use of echo in PHP?
It prints output to the browser or terminal.
Can PHP run without HTML?
Yes, PHP can run alone. But on websites, you often mix PHP with HTML.
What file extension should I use for PHP?
Use .php. Files with this extension can include and run PHP code.
What if I see the PHP code in the browser instead of the output?
It means the server is not processing PHP. Make sure you run the file on a server.
Similar Reads
PHP has a number of predefined constants that give you some information about the language and its environment. You can…
You can use your Raspberry Pi as a personal web server or tool that runs in your home. You do…
You may need to cut the last character from a string if it shows by mistake in PHP. That may…
PHP offers four main functions to include files: require, require_once, include, and include_once. Each one gives you a similar purpose…
PHP array_intersect_uassoc checks two or more arrays and returns matches. It compares both the values and the keys with a…
The PHP do-while loop is a type of loop that executes a block of code at least once, and then…
let's now dive into PHP comments—these simple but super important integral parts of PHP coding. You can think of comments…
One of the important tasks is retrieving documents from a collection. MongoDB help us to make this process, but understanding…
The array_combine function in PHP creates a new array with one array for keys and another for values. It needs…
The PHP array_intersect function finds common values in arrays and returns matches as a new array. Understand the array_intersect in…