Web Analytics

Your First PHP Program

Beginner~20 min read

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:

Output
Click Run to execute your code
Code Breakdown:
  • <?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
Important: Always use <?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:

Output
Click Run to execute your code
echo vs print: PHP has both 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>
How It Works: The server processes PHP code first, replacing it with output, then sends pure HTML to the browser. The browser never sees your PHP code!

Creating Your First PHP File

Follow these steps to create and run your first PHP file:

  1. Create a new file named hello.php
  2. 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
  3. Add this code:
    <?php
    echo "Hello from my first PHP file!";
    ?>
  4. Start your web server (Apache in XAMPP)
  5. Open your browser and visit: http://localhost/hello.php
Pro Tip: PHP files must have the .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:

  1. Browser requests hello.php from the server
  2. Apache web server sees the .php extension
  3. Server passes the file to PHP interpreter
  4. PHP executes the code and generates output
  5. Server sends the output (HTML) to your browser
  6. Browser displays the result
Key Point: PHP runs on the SERVER, not in the browser. This means:
  • 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.