Introduction to PHP

Welcome to your first PHP lesson! PHP (Hypertext Preprocessor) is one of the most popular server-side languages, powering over 75% of websites including WordPress, Facebook, and Wikipedia.

What is PHP?

PHP is a server-side scripting language designed specifically for web development. It's embedded in HTML and executed on the server before being sent to the browser.

Key Features

Basic Syntax

<?php
// Variables start with $
$name = "PHP";
$count = 0;
$isActive = true;

// Echo outputs text
echo "Hello, $name!";

// Arrays
$fruits = ["apple", "banana", "orange"];
$user = ["name" => "Alice", "age" => 25];
?>

Functions

<?php
// Function declaration
function greet($name) {
    return "Hello, " . $name . "!";
}

// Arrow functions (PHP 7.4+)
$add = fn($a, $b) => $a + $b;

// Array functions
$numbers = [1, 2, 3, 4, 5];
$doubled = array_map(fn($n) => $n * 2, $numbers);
?>

Object-Oriented PHP

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

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

    public function greet(): string {
        return "Hello, {$this->name}!";
    }
}

$user = new User("Alice", "alice@example.com");
echo $user->greet();
?>

Why Learn PHP?

  1. Web-focused - Built specifically for web development
  2. Easy to deploy - Runs on almost any hosting provider
  3. Huge ecosystem - Laravel, Symfony, WordPress, and more
  4. Active community - Modern PHP (8.x) is fast and feature-rich

Getting Started

In the next lesson, we'll set up a PHP development environment and write our first web page.

Introduction to PHP | LearningPHP.org