PHP Hello World Program - Complete Beginner Guide

Mahesh Mahesh Waghmare
2 min read

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)
Advertisement

Basic Hello World

Create PHP File

Create a file named hello.php:

<?php
echo "Hello, World!";
?>

Explanation

  • <?php - Opening PHP tag
  • echo - 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

  1. Save file to htdocs or www folder
  2. Start Apache
  3. Access: http://localhost/hello.php

Command Line Execution

Run PHP File

php hello.php

Output: Hello, World!

Interactive Mode

php -r "echo 'Hello, World!';"
Advertisement

Next Steps

After Hello World, learn:

  1. Variables: Store and use data
  2. Data Types: Strings, numbers, arrays
  3. Functions: Reusable code blocks
  4. Control Structures: If/else, loops
  5. Forms: Handle user input

Conclusion

Creating Hello World in PHP:

  1. Create PHP file with .php extension
  2. Use <?php opening tag
  3. Use echo to output text
  4. Run via web server or command line

Key Points:

  • PHP files use .php extension
  • Code between <?php and ?>
  • echo outputs text
  • Can run on server or CLI

Congratulations! You’ve created your first PHP program. Continue learning variables, functions, and more advanced concepts.

Advertisement
Mahesh Waghmare

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

Read Next