PHP - Hello World: Your First Step into Programming

Hello, aspiring programmers! I'm excited to be your guide on this journey into the world of PHP. As someone who's been teaching computer science for over a decade, I can tell you that the first "Hello World" program is always a special moment. It's like taking your first step on a new planet - simple, yet exhilarating! So, let's embark on this adventure together.

PHP - Hello World

What is PHP?

Before we dive into coding, let's understand what PHP is. PHP stands for "PHP: Hypertext Preprocessor" (yes, it's a recursive acronym - programmers love their jokes!). It's a server-side scripting language designed primarily for web development. Don't worry if these terms sound alien to you; by the end of this tutorial, you'll be familiar with them.

Setting Up Your PHP Environment

To write and run PHP code, you need a PHP interpreter installed on your computer. If you're using Windows, I recommend installing XAMPP, which includes PHP along with other useful tools. On Mac or Linux, PHP might already be installed. You can check by opening your terminal and typing:

php -v

If you see version information, you're good to go!

Your First PHP Script: Hello World

Creating the Script

Let's create our first PHP script. Open your favorite text editor (Notepad++ or Sublime Text are great choices for beginners) and type the following:

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

Save this file as hello_world.php in a location you can easily find.

Understanding the Code

Let's break down what each part of this tiny script does:

  1. <?php: This opening tag tells the PHP interpreter that PHP code follows.
  2. echo: This is a PHP command that outputs text.
  3. "Hello, World!": This is the text we want to display. In programming, text enclosed in quotes is called a "string".
  4. ;: In PHP, we end each statement with a semicolon. Think of it as the period at the end of a sentence.
  5. ?>: This closing tag tells PHP that we're done with the PHP code.

Running Your PHP Script

From the Command Prompt

Now, let's run our script! Open your command prompt (or terminal on Mac/Linux) and navigate to the directory where you saved your hello_world.php file. Then, type:

php hello_world.php

If everything is set up correctly, you should see "Hello, World!" printed in your command prompt. Congratulations! You've just run your first PHP script!

Understanding What Happened

When you ran the command, here's what happened behind the scenes:

  1. The PHP interpreter read your file.
  2. It saw the <?php tag and knew to start interpreting the code as PHP.
  3. It executed the echo command, which output the text.
  4. It reached the ?> tag and stopped interpreting PHP.

Expanding Your Hello World

Let's make our script a bit more interesting:

<?php
    $name = "Alice";
    echo "Hello, $name!";
    echo "\nWelcome to the world of PHP!";
?>

Save this as hello_name.php and run it. You should see:

Hello, Alice!
Welcome to the world of PHP!

What's New Here?

  1. $name = "Alice";: We've created a variable named name and assigned it the value "Alice".
  2. echo "Hello, $name!";: PHP replaces $name with its value when it's inside double quotes.
  3. \n: This is a special character that creates a new line.

PHP Variables and Data Types

In the example above, we used a variable. Variables in PHP always start with a $ sign. They can store different types of data:

Data Type Example Description
String $name = "John" Text
Integer $age = 25 Whole numbers
Float $height = 5.9 Decimal numbers
Boolean $isStudent = true True or False
Array $fruits = array("Apple", "Banana") Collection of values

Let's use some of these in a script:

<?php
    $name = "John";
    $age = 25;
    $height = 5.9;
    $isStudent = true;

    echo "Name: $name\n";
    echo "Age: $age\n";
    echo "Height: $height\n";
    echo "Is a student: " . ($isStudent ? "Yes" : "No") . "\n";
?>

Save this as variables.php and run it. You'll see how PHP handles different types of data.

Conclusion

Congratulations! You've taken your first steps into the world of PHP programming. We've covered creating and running a basic PHP script, using variables, and working with different data types. This is just the beginning of your journey.

Remember, learning to code is like learning a new language. It takes practice, patience, and persistence. Don't be afraid to experiment with the code examples, modify them, and see what happens. That's how you truly learn and grow as a programmer.

In my years of teaching, I've seen countless students go from writing their first "Hello World" to building complex web applications. Each journey started with this simple step. So, keep exploring, keep coding, and most importantly, have fun!

In our next lesson, we'll dive deeper into PHP control structures like if-statements and loops. Until then, happy coding!

Credits: Image by storyset