PHP is one of the most popular open-source and server-side scripting language. PHP is a powerful tool for making dynamic, interactive websites and you can do much more with it.
PHP Hello World is a small first program that confirms your PHP installation and introduces the basic shape of a PHP script. The example is simple, but it teaches an important distinction: PHP code runs on a server, while the browser receives the generated HTML.
This first exercise also gives you a safe place to practice saving files, reading error messages, and changing one line at a time. Once it works, you can move on to variables, conditions, forms, and database-backed pages.
Why This PHP Hello World Example Matters
A small test proves the setup
If the message appears in the browser, the server has processed the file and returned the result. That makes the example a useful checkpoint before you begin a larger project.

This guide starts with a plain text response and then places PHP inside an HTML page. You will also learn how to run the file locally, display variables, understand the difference between echo and print, and diagnose the most common beginner errors.

What PHP Does Before the Browser Receives a Page
Server-side execution
When a browser requests a PHP URL, a web server passes the file to the PHP runtime. PHP evaluates the instructions and sends the resulting response back to the browser. The browser does not need to understand PHP syntax because it receives the output, usually HTML.
This is why opening a .php file directly from a file manager is not the same as requesting it through a local server. A local server gives PHP the execution environment it needs.
What you need to begin
- A PHP installation or a local development bundle.
- A code editor that can display PHP syntax clearly.
- A project folder served by a local web server.
- A browser for checking the generated page.
Create a Correct PHP File
Use the PHP file extension
Save the file with a .php extension, such as hello.php. The extension helps the server decide that the file should be processed by PHP rather than returned as an ordinary text document.
Open and close PHP tags
PHP code begins with <?php. The closing tag is optional when a file contains only PHP, and leaving it out can prevent accidental whitespace from being sent before headers. Never place the dots from the original placeholder syntax inside a real PHP block.
<?php
echo 'Hello, World!';
Use echo for the First Output
Print a text value
The echo construct sends text to the response. A semicolon ends the instruction. Text is placed inside matching single or double quotation marks.
<?php
echo 'Hello, World!';
?>
Choose readable quotation marks
Single quotes are convenient when the text contains no variables that must be expanded. Double quotes can interpolate variables, but beginners should use them deliberately and escape any quotation marks needed inside the text.
Place PHP Inside HTML
Build a complete page
PHP can be embedded in a normal HTML document. The server processes the PHP block and leaves the surrounding HTML in the response.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My first PHP page</title>
</head>
<body>
<h1><?php echo 'Hello, World!'; ?></h1>
</body>
</html>
Keep output meaningful
Use headings and paragraphs for the page structure, and keep the PHP expression short when it only supplies a value. As the application grows, separate reusable logic from the template so the page remains easy to read.
Run PHP Hello World Locally
Use the built-in development server
If PHP is installed, open a terminal in the project directory and start the local server with the PHP command. The terminal must remain open while you test the page.
php -S localhost:8000
Then open http://localhost:8000/hello.php in the browser. If the file is inside a subfolder, include that folder in the address.
Use a local web stack
Applications such as local Apache-based bundles serve files from a document root. Put hello.php in the correct project directory, start the web server, and request it through the local host name rather than through a file:// address.
Display Variables and Dynamic Text
Create a variable
A variable stores a value for later use and begins with a dollar sign. Variable names are case-sensitive. Use names that explain what the value represents.
<?php
$name = 'Asha';
echo 'Hello, ' . $name . '!';
?>
Escape output when needed
For plain text created by your own code, the example is straightforward. When a value comes from a visitor or database, escape it for the output context with an appropriate HTML encoder before placing it in a page. Do not assume that a value is safe because it came from your own form.
Echo and Print Compared
When echo is useful
echo is commonly used to write one or more pieces of output. It is a natural choice for a small template expression.
When print is useful
print also writes text, but it behaves like an expression and returns a value. The difference rarely matters in a basic page, so consistency and readability are more useful criteria than trying to optimize the first example.
Common PHP Syntax Mistakes
Missing semicolons and quotes
A missing semicolon can cause a parse error on the next line. An unmatched quotation mark can make PHP treat the rest of the file as part of a string. Check the line reported by the error and the line immediately before it.
Incorrect tags or file type
If the browser displays the PHP code, confirm that the file ends in .php, that it is being requested through a PHP-enabled server, and that the opening tag is exactly <?php.
PHP Hello World Troubleshooting
Blank page or server error
Check the server’s error log and enable suitable development error reporting only on a local environment. Do not display detailed errors on a public site because they can reveal implementation information.
Wrong page or old output
Confirm the URL, project folder, and file name. Save the file before refreshing, and check whether a browser or proxy is showing a cached response. A simple visible marker can help confirm which file is being served.
Beginner Best Practices
Keep the first script small
Start with one output statement, then add one variable and one HTML element. Small changes make it easier to identify whether a problem comes from PHP syntax, the server, or the page structure.
Use clear code and comments
- Use meaningful variable names.
- Indent embedded HTML consistently.
- Remove temporary debugging output after testing.
- Keep secrets and database credentials out of beginner examples.
It can be learned easily. It is also important to know PHP is an object-oriented programming language. PHP runs on different operating systems (OS), like Windows, Linux and supports different databases like MySQL, Oracle and many more.
PHP can not only collect form data, but it can also create, read, write, delete, and close files on the server. PHP is a widely-used language for web development.
>Let’s start with the syntax first, but before this you have to create a file with .php extension. Now write PHP code to output the text “Hello world” to a browser:
First of all you need to know how to start php code in how to end :
//start php
<?php
.
.
.
.
?>
//end php
Now you need a file to write this php code:
welcome.php
<!DOCTYPE html>
<html>
<head>
<title>Title of the page</title>
</head>
<body>
</body>
</html>
After create this file insert php code:
<!DOCTYPE html>
<html>
<head>
<title>Title of the page</title>
</head>
<body>
<h1>
<?php
echo 'Hello World';
?>
</h1>
</body>
</html>
Write a comment