Best PHP Tutorial: Step-by-Step Guide for Beginners

PHP is a powerful and widely used scripting language for web development. If you want to master PHP, this guide will take you step by step through the basics to advanced concepts.

1. What is PHP?

PHP (Hypertext Preprocessor) is an open-source, server-side scripting language used to create dynamic web pages.

Why Learn PHP?

✅ Easy to learn
✅ Used in WordPress, Joomla, and Drupal
✅ Supports databases like MySQL and PostgreSQL
✅ Runs on all major operating systems

2. Setting Up Your PHP Environment

Before you start coding, install the necessary tools:

Step 1: Install a Local Server

Choose any of the following:

  • XAMPP (Windows, macOS, Linux) – Recommended
  • WAMP (Windows)
  • MAMP (macOS)

Step 2: Install a Code Editor

Popular editors for PHP development:

  • VS Code
  • Sublime Text
  • PHPStorm

Step 3: Create a PHP File

  • Open your text editor
  • Save a new file as index.php
  • Place it inside the htdocs (XAMPP) or www (WAMP) folder

3. Writing Your First PHP Script

Open index.php and add the following code:

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

How It Works:

  • <?php opens PHP code
  • echo prints text to the browser
  • ?> closes PHP code

Save the file and run it in a browser:
http://localhost/index.php

4. PHP Syntax Basics

Variables in PHP

Variables in PHP start with $:

<?php
    $name = "John";
    $age = 25;
    echo "Name: $name, Age: $age";
?>

PHP Data Types

PHP supports multiple data types:

  • String: $name = "John";
  • Integer: $age = 25;
  • Float: $price = 9.99;
  • Boolean: $is_active = true;
  • Array: $fruits = ["Apple", "Banana", "Cherry"];

5. PHP Control Structures

If-Else Statement

 
<?php
    $age = 18;
    if ($age >= 18) {
        echo "You are an adult.";
    } else {
        echo "You are underage.";
    }
?>

Loops in PHP

For Loop

<?php
    for ($i = 1; $i <= 5; $i++) {
        echo $i . "<br>";
    }
?>

While Loop

<?php
    $i = 1;
    while ($i <= 5) {
        echo $i . "<br>";
        $i++;
    }
?>

6. PHP Functions

Functions allow code reuse:

<?php
    function greet($name) {
        return "Hello, $name!";
    }
    echo greet("Alice");
?>

7. Working with Arrays

Indexed Array

<?php
    $colors = ["Red", "Green", "Blue"];
    echo $colors[0]; // Outputs: Red
?>

Associative Array

<?php
    $person = ["name" => "John", "age" => 25];
    echo $person["name"]; // Outputs: John
?>

Looping Through an Array

 
<?php
    $fruits = ["Apple", "Banana", "Cherry"];
    foreach ($fruits as $fruit) {
        echo $fruit . "<br>";
    }
?>

8. Handling Forms in PHP

Create a simple form:

HTML Form

<form method="post" action="process.php">
    Name: <input type="text" name="name">
    <input type="submit" value="Submit">
</form>

Processing the Form in PHP

Create process.php:

 
<?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $name = $_POST["name"];
        echo "Hello, " . htmlspecialchars($name);
    }
?>

9. Connecting PHP to a MySQL Database

Step 1: Connect to MySQL

<?php
    $conn = new mysqli("localhost", "root", "", "test_db");
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";
?>

Step 2: Insert Data into Database

<?php
    $sql = "INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com')";
    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $conn->error;
    }
?>

Step 3: Retrieve Data from Database

 
<?php
    $result = $conn->query("SELECT * FROM users");
    while ($row = $result->fetch_assoc()) {
        echo "Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>";
    }
?>

10. Debugging in PHP

To display errors, enable error reporting:

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
?>