Courses/PHP/Introduction to PHP

    Lesson 1 โ€ข Beginner

    Introduction to PHP ๐Ÿ˜

    Learn what PHP is, how it runs on the server, and why it powers over 77% of the web โ€” from WordPress to Facebook.

    What You'll Learn in This Lesson

    • โ€ข What PHP is and why it's the most popular server-side language
    • โ€ข How server-side code differs from client-side JavaScript
    • โ€ข The request-response cycle: browser โ†’ server โ†’ PHP โ†’ HTML โ†’ browser
    • โ€ข PHP syntax basics: echo, variables, comments, and string concatenation
    • โ€ข Setting up your PHP development environment

    1๏ธโƒฃ What is PHP?

    PHP (Hypertext Preprocessor) is a server-side scripting language designed for web development. Unlike JavaScript which runs in the browser, PHP runs on the web server and generates HTML that gets sent to the user.

    FeatureDetails
    Created1995 by Rasmus Lerdorf
    Latest VersionPHP 8.3 (2024)
    Market Share77% of websites with known server-side language
    Famous UsersWordPress, Wikipedia, Facebook, Slack
    File Extension.php

    2๏ธโƒฃ PHP Syntax Basics

    PHP code is written inside <?php ?> tags and can be embedded directly in HTML files:

    <!DOCTYPE html>
    <html>
    <body>
      <h1>Welcome</h1>
      <?php
        $name = "Alice";
        echo "<p>Hello, $name!</p>";
      ?>
    </body>
    </html>

    Key syntax rules:

    • Every statement ends with a semicolon ;
    • Variables always start with a dollar sign $
    • Strings are concatenated with the dot operator . not +
    • echo outputs text to the page

    Try It: Your First PHP Script

    See PHP basics โ€” variables, echo, comments, and string concatenation (simulated in JavaScript)

    Try it Yourself ยป
    JavaScript
    // PHP Introduction โ€” Simulated in JavaScript
    // In real PHP, code runs on the server inside <?php ?> tags
    
    console.log("=== Your First PHP Script ===");
    console.log();
    
    // In PHP, you use 'echo' to output text
    // echo "Hello, World!";
    console.log("Hello, World!");
    console.log();
    
    // PHP can mix with HTML on the server
    console.log("=== How PHP Embeds in HTML ===");
    console.log();
    console.log("<!DOCTYPE html>");
    console.log("<html>");
    console.log("<body>");
    console.log("  <h1>My First PHP Page</h
    ...

    3๏ธโƒฃ How PHP Works

    PHP follows a request โ†’ process โ†’ response cycle:

    Browser โ†’ HTTP Request โ†’ Web Server โ†’ PHP Interpreter
                                                โ†“
    Browser โ† HTML Response โ† Web Server โ† Generated HTML
    ๐Ÿ’ก
    Pro Tip: The user never sees your PHP code โ€” only the HTML output. This is why PHP is great for hiding business logic and database passwords.

    Try It: How PHP Works (Server-Side)

    Understand the request-response cycle and see PHP vs JavaScript compared

    Try it Yourself ยป
    JavaScript
    // How PHP Works โ€” Server-Side Execution
    console.log("=== How PHP Works: Step by Step ===");
    console.log();
    
    // Simulate the request-response cycle
    console.log("1๏ธโƒฃ User types: www.example.com/index.php");
    console.log("   โ†’ Browser sends HTTP request to server");
    console.log();
    
    console.log("2๏ธโƒฃ Web server (Apache/Nginx) receives the request");
    console.log("   โ†’ Finds index.php file on disk");
    console.log("   โ†’ Passes it to the PHP interpreter");
    console.log();
    
    console.log("3๏ธโƒฃ PHP interpreter 
    ...

    4๏ธโƒฃ Setting Up PHP

    Three ways to start coding PHP:

    1.
    Built-in Server (Easiest)
    php -S localhost:8000 โ€” no Apache needed!
    2.
    XAMPP/MAMP โ€” installs Apache + PHP + MySQL in one click
    3.
    Docker โ€” docker run -p 8080:80 php:8.3-apache

    โš ๏ธ Common Mistakes

    โš ๏ธ
    Forgetting the semicolon โ€” every PHP statement must end with ;
    โš ๏ธ
    Missing the $ on variables โ€” PHP requires $name, not just name
    โš ๏ธ
    Using + for string concatenation โ€” PHP uses the . dot operator
    ๐Ÿ’ก
    Pro Tip: Enable error reporting with error_reporting(E_ALL) during development to catch mistakes early.

    ๐Ÿ“‹ Quick Reference โ€” PHP Basics

    SyntaxExampleDescription
    echoecho "Hello";Output text
    $variable$name = "Alice";Declare variable
    . (dot)"Hi " . $nameConcatenate strings
    //// commentSingle-line comment
    <?php ?><?php echo "Hi"; ?>PHP opening/closing tags

    ๐ŸŽ‰ Lesson Complete!

    You now understand what PHP is and how it works on the server! Next, dive into PHP variables and data types.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service