"I am Saqib Jahangir. A passionate vlogger, software engineer, trainer and avid traveler with a deep love for exploring the hidden gems of our beautiful planet. With a strong foundation in Application Development, Application Architecture & Database Design and Product Management, I bring over a decade of hands-on experience building secure, scalable, and resilient web applications for a diverse range of industries."

Writing Your First PHP Script

 

1️ Introduction

PHP is a server-side scripting language that allows you to create dynamic web pages. Writing your first PHP script is simple, and it’s the first step toward building interactive websites.


2️ Setup

Before you begin:

  • Make sure Apache is running (via WAMP, XAMPP, or MAMP).
  • Place your PHP files in the correct folder:
    • WAMPC:\wamp64\www\
    • XAMPPC:\xampp\htdocs\

3️ Create Your First PHP File

  1. Open a text editor (VS Code, Sublime Text, or Notepad).
  2. Create a new file named index.php.
  3. Add the following code:

<?php

// This is a single-line comment

echo "Hello, World!";

?>


4️ Understanding the Code

  • <?php ... ?> → Marks the start and end of PHP code.
  • echo → Outputs text to the browser.
  • // → Single-line comment.

5️ Run Your Script

  1. Save the file in the correct folder (www or htdocs).
  2. Open a web browser and navigate to:

http://localhost/index.php

  1. You should see:

Hello, World!


6️ Adding HTML with PHP

You can mix HTML and PHP for dynamic pages:

<!DOCTYPE html>

<html>

<head>

    <title>My First PHP Page</title>

</head>

<body>

    <h1>

        <?php

            echo "Welcome to My First PHP Script!";

        ?>

    </h1>

</body>

</html>

  • PHP runs on the server and outputs HTML.
  • The browser only sees the result, not the PHP code.

Tips for Beginners

Always save files with .php extension.
Start with simple scripts using echo.
Use comments // or /* ... */ to document your code.
Combine PHP with HTML to create dynamic web pages.

 

Popular Posts

Operators (Arithmetic, Comparison, Logical)

Functions (Built-in & User-defined)