PHP Hello World Program - Complete Beginner Guide
Mahesh Waghmare Creating a “Hello World” program is the traditional first step in learning any programming language. This guide walks you through creating your first PHP program.
Introduction
A “Hello World” program is the simplest program that outputs text. It’s the perfect starting point for learning PHP syntax and execution.
What You’ll Learn:
- Basic PHP syntax
- How to run PHP code
- Output methods
- File structure
Prerequisites
Before starting, ensure you have:
- PHP installed (see PHP installation guide)
- Text editor (VS Code, Notepad++, etc.)
- Web server (for web execution) OR
- Command line access (for CLI execution)
Basic Hello World
Create PHP File
Create a file named hello.php:
<?php
echo "Hello, World!";
?>
Explanation
<?php- Opening PHP tagecho- Output statement"Hello, World!"- String to display?>- Closing PHP tag (optional in modern PHP)
Simplified Version
In modern PHP, closing tag is optional:
<?php
echo "Hello, World!";
Running on Web Server
Using Built-in Server
Start PHP server:
php -S localhost:8000
Access: http://localhost:8000/hello.php
Using XAMPP/WAMP
- Save file to
htdocsorwwwfolder - Start Apache
- Access:
http://localhost/hello.php
Command Line Execution
Run PHP File
php hello.php
Output: Hello, World!
Interactive Mode
php -r "echo 'Hello, World!';"
Next Steps
After Hello World, learn:
- Variables: Store and use data
- Data Types: Strings, numbers, arrays
- Functions: Reusable code blocks
- Control Structures: If/else, loops
- Forms: Handle user input
Conclusion
Creating Hello World in PHP:
- Create PHP file with
.phpextension - Use
<?phpopening tag - Use
echoto output text - Run via web server or command line
Key Points:
- PHP files use
.phpextension - Code between
<?phpand?> echooutputs text- Can run on server or CLI
Congratulations! You’ve created your first PHP program. Continue learning variables, functions, and more advanced concepts.
Written by Mahesh Waghmare
I bridge the gap between WordPress architecture and modern React frontends. Currently building tools for the AI era.
Follow on Twitter →