Welcome to the exciting world of PHP! 🚀 If you’re just starting your journey in web development, you’ve made an excellent choice. PHP is a versatile and powerful server-side scripting language that powers millions of websites worldwide. In this article, we’ll guide you through creating your very first PHP script – the classic “Hello World” program.

What is PHP?

Before we dive into coding, let’s briefly understand what PHP is. PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

💡 Fun Fact: PHP was created by Rasmus Lerdorf in 1994. It was originally designed as a set of Common Gateway Interface (CGI) binaries written in the C programming language.

Setting Up Your Environment

To run PHP scripts, you need a web server with PHP installed. For beginners, we recommend using XAMPP, which provides an easy-to-install Apache distribution containing MariaDB, PHP, and Perl.

  1. Download XAMPP from the official website.
  2. Install XAMPP on your computer.
  3. Start the Apache server from the XAMPP control panel.

Creating Your First PHP Script

Now that we have our environment set up, let’s create our first PHP script!

  1. Open your favorite text editor (like Notepad++, Sublime Text, or Visual Studio Code).
  2. Create a new file and save it as hello_world.php in the htdocs folder of your XAMPP installation (usually C:\xampp\htdocs on Windows).

Here’s the code for our first PHP script:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My First PHP Script</title>
</head>
<body>
    <h1>
        <?php
        echo "Hello, World!";
        ?>
    </h1>
</body>
</html>

Let’s break down this code:

  • The <!DOCTYPE html> declaration and HTML tags create the structure of our web page.
  • Inside the <body> tag, we have an <h1> heading.
  • The <?php ?> tags indicate the start and end of PHP code.
  • The echo statement is used to output text.

Running Your PHP Script

To see your script in action:

  1. Make sure your Apache server is running in XAMPP.
  2. Open a web browser and navigate to http://localhost/hello_world.php.

You should see a web page with “Hello, World!” displayed as a heading.

🎉 Congratulations! You’ve just run your first PHP script!

Understanding PHP Syntax

Now that we’ve created a basic script, let’s explore some fundamental PHP syntax:

PHP Tags

PHP code is enclosed in special tags:

<?php
// Your PHP code here
?>

For short echo statements, you can use the short echo tag:

<?= "Hello, World!" ?>

Statements and Semicolons

Each PHP statement ends with a semicolon:

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

Variables

In PHP, variables start with a $ sign:

<?php
$name = "John";
echo "Hello, $name!";
?>

Comments

PHP supports both single-line and multi-line comments:

<?php
// This is a single-line comment

/*
This is a
multi-line comment
*/
?>

Expanding Our Hello World

Let’s enhance our Hello World script to demonstrate more PHP features:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Enhanced Hello World</title>
</head>
<body>
    <?php
    // Define variables
    $visitor = "CodeLucky Learner";
    $current_time = date("H:i:s");
    $current_date = date("Y-m-d");

    // Function to get greeting based on time
    function getGreeting($hour) {
        if ($hour < 12) {
            return "Good morning";
        } elseif ($hour < 18) {
            return "Good afternoon";
        } else {
            return "Good evening";
        }
    }

    // Get current hour
    $current_hour = date("H");
    $greeting = getGreeting($current_hour);
    ?>

    <h1><?= "$greeting, $visitor!" ?></h1>
    <p>Welcome to your enhanced Hello World script.</p>
    <p>The current time is: <?= $current_time ?></p>
    <p>Today's date is: <?= $current_date ?></p>

    <?php
    // Demonstrate a simple loop
    echo "<h2>Counting to 5:</h2>";
    echo "<ul>";
    for ($i = 1; $i <= 5; $i++) {
        echo "<li>Number $i</li>";
    }
    echo "</ul>";
    ?>
</body>
</html>

This enhanced script demonstrates:

  • Variable declaration and usage
  • PHP’s built-in date() function
  • Custom function definition and calling
  • Conditional statements (if-elseif-else)
  • String concatenation
  • For loop
  • Mixing HTML and PHP

When you run this script, you’ll see a personalized greeting, the current time and date, and a numbered list – all dynamically generated by PHP!

Output

Here’s what the output might look like (assuming it’s afternoon):

Good afternoon, CodeLucky Learner!

Welcome to your enhanced Hello World script.
The current time is: 14:30:45
Today's date is: 2023-06-15

Counting to 5:

1. Number 1
2. Number 2
3. Number 3
4. Number 4
5. Number 5

Conclusion

Congratulations! 🎊 You’ve not only created your first PHP script but also expanded it to include various PHP features. This is just the beginning of your PHP journey. As you continue to learn, you’ll discover how PHP can help you create dynamic, interactive websites and web applications.

Remember, practice is key in programming. Try modifying the scripts we’ve created, experiment with different PHP functions, and don’t be afraid to make mistakes – they’re an essential part of the learning process!

Happy coding, and welcome to the world of PHP development! 💻🐘