Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

PHP: The Web's Kitchen Workhorse

Welcome to the PHP language guide! If programming languages were kitchens, PHP would be the bustling, reliable kitchen that powers a huge portion of the web's restaurants, from small cafes to massive food chains.

What is PHP?

PHP (a recursive acronym for "PHP: Hypertext Preprocessor") is a server-side scripting language designed specifically for web development. "Server-side" means it runs on the web server (the "kitchen"), not in the user's browser. It processes requests, interacts with databases, and generates HTML to send back to the customer's screen.

What is it Used For?

PHP is a dominant force on the web. Its primary uses include:

  • Building Dynamic Websites: Creating interactive and data-driven web pages.
  • Powering Content Management Systems (CMS): It's the engine behind giants like WordPress, Drupal, and Joomla, which together run a massive percentage of all websites.
  • E-commerce Platforms: Powering online stores like Magento and WooCommerce.
  • Backend API Development: Creating the backend logic for web and mobile applications.
  • Command-Line Scripting: Writing scripts to automate server tasks.

Why You Might Like PHP

  • Built for the Web: Its features are tailored for building websites, making common web tasks straightforward.
  • Gentle Learning Curve: The syntax is flexible and forgiving, which can make it easier for beginners to get started and see results quickly.
  • Massive Ecosystem: Because it has been around for a long time, there is a vast amount of documentation, tutorials, and libraries available.
  • Easy to Deploy: Most web hosting providers support PHP out of the box, making it simple to get your project online.

Keep in Mind

While modern PHP is robust, it has a legacy of older code and tutorials online, so it's important to ensure you're learning the latest best practices.

A Taste of PHP Syntax

Here’s a taste of what PHP code looks like. It's often mixed directly with HTML, like a recipe card with instructions written right on it.

<?php
// === PHP: A Day at The Coder's Cafe ===

// --- Module 1: Greeting the Customer ---
echo "Welcome to The Coder's Cafe!\n";
// Taking an order is like getting user input.
$customer_name = "Rasmus"; // This is a note for the chef (a comment)

// --- Module 2: Prepping the Ingredients (Data) ---
$dish_name = "PHP Hypertext Pancakes";   // String
$quantity = 3;                           // Integer
$price_per_dish = 8.50;                  // Float
$is_order_ready = false;                 // Boolean
$order_summary = "{$quantity}x {$dish_name}"; // String Interpolation

// --- Module 3: In the Kitchen (Logic) ---
if (str_contains($dish_name, "Pancakes")) {
    echo "Cooking {$order_summary} on the griddle.\n";
} else {
    echo "Cooking {$order_summary} on the stove.\n";
}

// --- Module 4: Handling the Full Order (Collections & Loops) ---
// A customer's complete order (Array)
$customer_order_list = ["PHP Hypertext Pancakes", "Server-Side Smoothie"];
echo "Processing full order:\n";
foreach ($customer_order_list as $item) {
    echo "- Adding {$item} to the ticket.\n";
}

// A process that repeats until a condition is met (While Loop)
$soup_temp = 80;
while ($soup_temp < 100) {
    echo "Heating soup... now at {$soup_temp}°C\n";
    $soup_temp += 10;
}
echo "Soup is ready!\n";

// --- Module 5: The Final Bill & A Special Offer (Functions & Imports) ---
// The rand() function is built-in, no import needed.

// A standard procedure (Function)
function calculate_bill($customer, $items, $total_price) {
    echo "\n--- Bill for {$customer} ---\n";
    foreach ($items as $item) {
        echo "  - {$item}\n";
    }

    // Let's add a random promotional discount!
    $discount = rand(5, 20); // 5% to 20% off
    echo "Applying a special {$discount}% discount!\n";
    $final_price = $total_price * (1 - $discount / 100);
    return $final_price; // Return the calculated value
}

// A bill represented as an Associative Array (Key-Value pairs)
$order_bill = [
    "customer" => $customer_name,
    "items" => $customer_order_list,
    "total" => $price_per_dish * $quantity
];

// Call the function to get the final result
$final_amount = calculate_bill(
    $order_bill["customer"],
    $order_bill["items"],
    $order_bill["total"]
);

echo "Your final bill is $" . number_format($final_amount, 2) . ".\n";
echo "Thank you for dining with us, {$customer_name}!\n";
?>

Start Coding in PHP

Here are the simplest ways to start, from the easiest method to the most common one.

1. In Your Browser (The Easiest Start)

This method requires no installation. You can write and run small PHP scripts immediately on a website.

  • What to use: any "PHP Sandbox" website: PHP Sandbox, php-playground
  • How it works: You type your PHP code (e.g., <?php echo "Hello World!"; ?>) in a text box, click the "Run" button, and see the output.
  • Best for: Learning the PHP syntax and testing a small piece of code.

2. On Your Computer (The Standard Way)

PHP is a "server-side" language, which means it almost always runs on a "web server" to generate an HTML page that is then sent to your browser.

A. The "All-in-One" Method (Easiest Local Setup)

This is the most common way for beginners to set up a complete local development environment.

  • What to use: XAMPP (for Windows/Mac/Linux)
  • How it works:
    1. You install XAMPP. This one program gives you Apache (a web server), PHP, and a database (MariaDB).
    2. You start the "Apache" server from the XAMPP control panel.
    3. You place your .php files in a special folder (e.g., C:\xampp\htdocs).
    4. You view your work by visiting http://localhost/your-file-name.php in your web browser.
  • Best for: Beginners, learning how PHP builds web pages, and working with databases.

B. Using an IDE for a Better Workflow

An "IDE" (Integrated Development Environment) is a powerful editor with extra features that help you write better code, faster.

C. Managing Project Dependencies with Composer

This is the modern, standard way to build any serious PHP application.

  • What is a dependency? Most PHP projects use "packages" (or libraries), which are collections of pre-written code that solve common problems. You can find these on Packagist, the main online repository for PHP packages.
  • What tool to use: Composer
  • How it works: Composer is PHP's dependency manager. You declare the libraries your project needs in a composer.json file. Composer then downloads and manages them for you. This is the foundation for all modern PHP frameworks.
  • Examples of Key Packages/Frameworks you can install: