Hello World in PHP
Your first PHP program - the classic Hello World example with Docker setup
Every programming journey starts with Hello World. Let’s write our first PHP program.
The Code
Create a file named hello.php:
| |
Understanding the Code
<?php- The opening tag that tells the server to interpret what follows as PHP code.echo- A language construct that outputs one or more strings. It’s the most common way to output text in PHP."Hello, World!\n"- A string literal with a newline character at the end.- No closing tag - When a file contains only PHP code, it’s best practice to omit the closing
?>tag to avoid accidental whitespace issues.
Running with Docker
The easiest way to run this without installing PHP locally:
| |
Running Locally
If you have PHP installed:
| |
Expected Output
Hello, World!
Alternative Syntax
PHP offers several ways to output text:
| |
Key Concepts
- PHP is interpreted - Code is executed line by line by the PHP interpreter
- Designed for the web - PHP was created specifically for web development
- Embedded in HTML - PHP can be mixed with HTML in
.phpfiles - Flexible typing - Variables don’t require type declarations (though type hints are available in modern PHP)
PHP Tags
PHP code must be enclosed in tags:
| |
For files containing only PHP code (like libraries), omit the closing tag:
| |
Next Steps
Continue to Variables and Data Types to learn about storing and manipulating data in PHP.
Running Today
All examples can be run using Docker:
docker pull php:8.3-cli-alpineLast updated: