In this lesson, we will write our first PHP script. This will help you understand the basic structure of a PHP file and how to execute it in your development environment.

Prerequisites

Before you start, ensure you have:

  • A web server with PHP installed (e.g., XAMPP, WAMP, MAMP, or a live server).
  • A text editor or an Integrated Development Environment (IDE) like VS Code, Sublime Text, or PHPStorm.

Writing Your First PHP Script

Step 1: Create a PHP File

  1. Open your text editor or IDE.
  2. Create a new file and save it as first_script.php.

Step 2: Write the PHP Code

In your first_script.php file, type the following code:

<?php
    // This is a single-line comment
    echo "Hello, World!";
?>

Explanation

  • <?php: This tag opens a PHP block. All PHP code must be written inside these tags.
  • echo "Hello, World!";: This statement outputs the text "Hello, World!" to the browser. The echo command is used to display text or variables.
  • // This is a single-line comment: Comments are used to explain the code and are ignored by the PHP engine. Single-line comments start with //.
  • ?>: This tag closes the PHP block. It is optional if the PHP block is at the end of the file.

Step 3: Run Your PHP Script

  1. Save the first_script.php file in the root directory of your web server (e.g., htdocs for XAMPP).
  2. Open your web browser and navigate to http://localhost/first_script.php.

You should see the text "Hello, World!" displayed on the page.

Practical Exercise

Task

Create a PHP script that displays your name and the current date.

Solution

  1. Create a new file and save it as display_name_date.php.
  2. Write the following code:
<?php
    // Displaying name
    echo "My name is John Doe.<br>";

    // Displaying current date
    echo "Today's date is " . date("Y-m-d") . ".";
?>

Explanation

  • echo "My name is John Doe.<br>";: This statement outputs the text "My name is John Doe." followed by an HTML line break (<br>).
  • date("Y-m-d"): The date function returns the current date in the format YYYY-MM-DD.

Running the Script

  1. Save the display_name_date.php file in the root directory of your web server.
  2. Open your web browser and navigate to http://localhost/display_name_date.php.

You should see the text "My name is John Doe." followed by the current date.

Common Mistakes and Tips

  • Forgetting the PHP tags: Ensure your PHP code is enclosed within <?php and ?> tags.
  • Syntax errors: PHP statements end with a semicolon (;). Missing semicolons can cause errors.
  • File location: Make sure your PHP file is saved in the correct directory of your web server.

Summary

In this lesson, you learned how to:

  • Create a PHP file.
  • Write and run a simple PHP script.
  • Use the echo statement to display text.
  • Use the date function to display the current date.

Next, we will dive deeper into PHP syntax and variables, which are fundamental for writing more complex scripts.

PHP Programming Course

Module 1: Introduction to PHP

Module 2: Control Structures

Module 3: Functions

Module 4: Arrays

Module 5: Working with Forms

Module 6: Working with Files

Module 7: Object-Oriented Programming (OOP)

Module 8: Working with Databases

Module 9: Advanced PHP Techniques

Module 10: PHP Frameworks and Best Practices

Module 11: Project: Building a Web Application

© Copyright 2024. All rights reserved