Your First PHP Script: Hello World in 3 Minutes
A step-by-step beginner's guide to writing and running your first PHP script using tools like XAMPP, MAMP, Laragon, or Docker.
π What You'll Learn
If you're brand new to PHP, this tutorial will walk you through creating your first "Hello, World!" script. Youβll learn how to:
- Write a basic PHP file
- Run it on a local server
- Understand PHP syntax and output
π Prerequisites: Set Up a PHP Environment
To run PHP scripts, you'll need a web server with PHP installed. The easiest ways to get started are:
π Step 1: Create Your PHP File
Open your favorite code editor (e.g., VS Code, Sublime Text, Notepad++), and create a new file named index.php
in your web root directory:
<?php
echo "Hello, World!";
?>
This simple script tells the PHP engine to print the text Hello, World!
to your browser.
π Step 2: Place the File in Your Web Directory
Depending on your setup, place index.php
in one of the following folders:
- XAMPP:
C:\xampp\htdocs\hello
- Laragon:
C:\laragon\www\hello
- MAMP:
/Applications/MAMP/htdocs/hello
- Docker: Bind your local folder using
-v ${PWD}:/var/www/html
π Step 3: Run the Script
Start your local web server and visit:
http://localhost/hello
(XAMPP or Laragon)http://localhost:8888/hello
(MAMP)http://localhost:8080
(Docker)
You should see the text Hello, World! displayed on the page. π
π§ Understanding the Code
Letβs break it down:
<?php // Opening PHP tag
echo "Hello, World!"; // Output to browser
?> // Closing PHP tag
echo
is a PHP command that outputs strings or variables to the browser.
π What's Next?
- Try printing variables and using math operators.
- Explore if-else conditions and loops.
- Create a simple form that displays user input.
This is just the beginning of your PHP journey. Keep experimenting and building!
Tags: hello world PHP, PHP basics, first PHP script
π Continue learning PHP with more beginner-friendly tutorials on file uploads, form handling, and connecting to MySQL.