Your First PHP Program
Let's write your first PHP program! You'll learn how to create a PHP file, understand the basic structure, and see your code come to life in the browser.
The Classic "Hello, World!"
Every programming journey starts with "Hello, World!" - a simple program that displays text. Here's how it looks in PHP:
Click Run to execute your code
<?php- Opening PHP tag (tells server "PHP code starts here")echo- Outputs text to the browser"Hello, World!"- String (text) enclosed in quotes;- Semicolon ends the statement
PHP Tags Explained
PHP code must be wrapped in special tags so the server knows what to execute:
| Tag Type | Syntax | Usage |
|---|---|---|
| Standard (Recommended) | <?php ... ?> |
Always works, most portable |
| Short Echo | <?= ... ?> |
Shortcut for echo, PHP 5.4+ |
| Short Tags | <? ... ?> |
Deprecated, avoid using |
<?php for maximum
compatibility. Short tags may not work on all servers!
The echo Statement
echo is PHP's primary way to output content. It can display text,
numbers, HTML, and more:
Click Run to execute your code
echo and
print. They're nearly identical, but echo is slightly
faster and can output multiple values. Most developers use echo.
Mixing PHP with HTML
One of PHP's superpowers is seamlessly mixing with HTML. You can switch between HTML and PHP as needed:
<!DOCTYPE html>
<html>
<head>
<title>My PHP Page</title>
</head>
<body>
<h1>Welcome!</h1>
<?php
echo "<p>This paragraph was generated by PHP!</p>";
echo "<p>The current time is: " . date("H:i:s") . "</p>";
?>
<p>This is regular HTML</p>
</body>
</html>
Creating Your First PHP File
Follow these steps to create and run your first PHP file:
- Create a new file named
hello.php - Save it in your web server's document root:
- XAMPP Windows:
C:\xampp\htdocs\hello.php - XAMPP Mac:
/Applications/XAMPP/htdocs/hello.php - XAMPP Linux:
/opt/lampp/htdocs/hello.php
- XAMPP Windows:
- Add this code:
<?php echo "Hello from my first PHP file!"; ?> - Start your web server (Apache in XAMPP)
- Open your browser and visit:
http://localhost/hello.php
.php extension.
Files with .html extension won't be processed by PHP!
Understanding How PHP Executes
Here's what happens when you visit a PHP page:
- Browser requests
hello.phpfrom the server - Apache web server sees the
.phpextension - Server passes the file to PHP interpreter
- PHP executes the code and generates output
- Server sends the output (HTML) to your browser
- Browser displays the result
- Users can't see your PHP source code
- PHP can access server resources (databases, files)
- You need a web server to run PHP (can't just open .php files in browser)
Common Mistakes
1. Opening .php files directly in browser
// Wrong - opening file directly
file:///C:/xampp/htdocs/hello.php โ
// Correct - through web server
http://localhost/hello.php โ
PHP must be processed by a web server. Opening files directly shows the source code!
2. Forgetting the closing semicolon
<?php
echo "Hello" // โ Parse error!
?>
<?php
echo "Hello"; // โ
Correct
?>
3. Mixing up quotes
<?php
echo "Hello'; // โ Mismatched quotes
echo 'Hello"; // โ Mismatched quotes
echo "Hello"; // โ
Matching double quotes
echo 'Hello'; // โ
Matching single quotes
?>
4. Wrong file extension
hello.html โ Won't execute PHP code
hello.txt โ Won't execute PHP code
hello.php โ
Correct!
Exercise: Personalized Greeting
Task: Create a PHP program that displays a personalized greeting with the current date and time.
Requirements:
- Display "Welcome to PHP Programming!"
- Show your name
- Display the current date using
date("Y-m-d") - Display the current time using
date("H:i:s")
Show Solution
<?php
echo "Welcome to PHP Programming!";
echo "\n";
echo "My name is John Doe";
echo "\n";
echo "Today's date: " . date("Y-m-d");
echo "\n";
echo "Current time: " . date("H:i:s");
?>
Summary
- PHP Tags: Use
<?php ... ?>to wrap PHP code - echo: Outputs content to the browser
- Semicolons: Required at the end of each statement
- File Extension: PHP files must end with
.php - Execution: PHP runs on the server, not in the browser
- Access: Use
http://localhost/filename.php, not file:// - Mixing: PHP and HTML can be mixed in the same file
What's Next?
Congratulations on running your first PHP program! In the next lesson, we'll dive deeper into PHP syntax basics - comments, statements, case sensitivity, and the fundamental rules of writing clean PHP code.
Enjoying these tutorials?