Home Blog PHP PHP Fundamentals: From Zero to Hero
PHP Fundamentals: From Zero to Hero

PHP Fundamentals: From Zero to Hero

D
5 min read
Share:

Welcome to your comprehensive guide to PHP programming! Whether you're completely new to programming or coming from another language, this guide will take you from the basics to building real-world applications.

Table of Contents

Getting Started with PHP

PHP (Hypertext Preprocessor) is a server-side scripting language designed specifically for web development. Before diving into coding, you'll need:

  • A web server (like Apache or Nginx)
  • PHP installed (latest stable version recommended)
  • A text editor or IDE (VS Code, PHPStorm, or Sublime Text)

Pro Tip: Start with XAMPP or WAMP for an all-in-one development environment that includes PHP, Apache, and MySQL.

PHP Basics

Every PHP script starts with <?php and ends with ?>. Here's your first PHP program:

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

Save this as index.php in your web server's directory and access it through your browser.

Variables and Data Types

PHP variables start with a $ symbol. PHP supports several data types:

<?php
    // String
    $name = "John Doe";
    
    // Integer
    $age = 25;
    
    // Float/Double
    $price = 19.99;
    
    // Boolean
    $isStudent = true;
    
    // Array
    $colors = ["red", "green", "blue"];
    
    // Null
    $nothing = null;
?>
        

Control Structures

PHP provides essential control structures for program flow:

<?php
    // If-else statement
    $age = 20;
    if ($age >= 18) {
        echo "You are an adult";
    } else {
        echo "You are a minor";
    }

    // Loops
    for ($i = 0; $i < 5; $i++) {
        echo "Iteration: $i\n";
    }

    // While loop
    $counter = 0;
    while ($counter < 3) {
        echo "Count: $counter\n";
        $counter++;
    }
?>
        

Functions

Functions help organize and reuse code:

<?php
    function calculateArea($length, $width) {
        return $length * $width;
    }

    // Function with default parameter
    function greet($name = "Guest") {
        return "Hello, $name!";
    }

    // Using the functions
    echo calculateArea(5, 3);  // Outputs: 15
    echo greet("John");        // Outputs: Hello, John!
    echo greet();              // Outputs: Hello, Guest!
?>
        

Arrays and Array Functions

PHP offers powerful array manipulation capabilities:

<?php
    // Indexed array
    $fruits = ["apple", "banana", "orange"];

    // Associative array
    $person = [
        "name" => "John Doe",
        "age" => 25,
        "city" => "New York"
    ];

    // Multidimensional array
    $students = [
        ["name" => "John", "grade" => 85],
        ["name" => "Jane", "grade" => 92]
    ];

    // Array functions
    sort($fruits);                 // Sort indexed array
    array_push($fruits, "mango"); // Add element
    $length = count($fruits);     // Get array length
?>
        

Working with Forms

Handle HTML forms and user input:

<?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $username = $_POST["username"];
        $email = $_POST["email"];
        
        // Validate input
        if (empty($username)) {
            echo "Username is required";
        } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            echo "Invalid email format";
        } else {
            echo "Form submitted successfully";
        }
    }
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    Username: <input type="text" name="username">
    Email: <input type="email" name="email">
    <input type="submit">
</form>
        

Database Integration

Connect and interact with MySQL databases:

<?php
    // Database connection
    $conn = new mysqli("localhost", "username", "password", "database");

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Insert data
    $sql = "INSERT INTO users (name, email) VALUES (?, ?)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ss", $name, $email);
    $stmt->execute();

    // Select data
    $result = $conn->query("SELECT * FROM users");
    while ($row = $result->fetch_assoc()) {
        echo $row["name"] . " - " . $row["email"] . "\n";
    }
?>
        

Object-Oriented Programming

PHP supports object-oriented programming:

<?php
    class User {
        private $name;
        private $email;

        public function __construct($name, $email) {
            $this->name = $name;
            $this->email = $email;
        }

        public function getName() {
            return $this->name;
        }

        public function getEmail() {
            return $this->email;
        }
    }

    // Create an object
    $user = new User("John Doe", "john@example.com");
    echo $user->getName();  // Outputs: John Doe
?>
        

Next Steps

After mastering these fundamentals, you can:

  • Learn a PHP framework like Laravel or Symfony
  • Explore advanced topics like security and optimization
  • Build real-world web applications
  • Practice with small projects to reinforce your learning
D

About Daniel

Technical writer and developer at DigitalCodeLabs with expertise in web development and server management.