Introduction to Dynamic Web App Development with PHP

Open Source Concepts

  • Freeware vs. Open Source:
    • Freeware: Software that is free to use, but users do not have access to the source code of the application.
    • Open Source: Software where users have the ability to view, inspect, and modify the source code. An open-source project typically accepts modifications and contributions from the community.

Static vs. Dynamic Web Sites

Comparison between static and dynamic websites

  • Static Webpages:

    • Content remains fixed and stable; it does not change based on user requests or identity.
    • Consists of fixed content stored directly on the web server and pulled as-is, delivered to the user without server-side processing.
    • Content changes must be updated manually page by page.
    • Fast to create initially, but requires more intensive content management as the website grows.
    • Functionality and user interaction capabilities are limited.
  • Dynamic Webpages:

    • Generates content on the fly using server-side processing and databases.
    • Content is stored in a database or collection and delivered according to how it is organized or filtered.
    • Changes can be applied automatically across hundreds of pages at once.
    • May take longer to set up initially, but long-term management and scaling are much more efficient.
    • Content displays dynamically based on user interactions, user inputs, or personalized viewing parameters.

Overview of PHP

  • Definition: PHP is a widely used, general-purpose open-source scripting language designed specifically for web development.

  • Acronym Meaning:

    • Originally stood for Personal Home Pages.
    • Now stands for PHP: Hypertext Preprocessor (a recursive acronym).
  • Key Characteristics:

    • PHP scripts are executed on the web server.
    • It is completely free to download and use from the official resource (www.php.net).
    • Easy to learn and executes efficiently on the server side.
  • Capabilities of PHP:

    • Generates dynamic web page content on the fly.
    • Creates, opens, reads, writes, deletes, and closes files directly on the server.
    • Collects and processes HTML form data.
    • Sends and receives HTTP cookies.
    • Adds, deletes, and modifies data stored within databases.
    • Controls user access permissions across different sections of a web application.
    • Encrypts sensitive data.
  • Platform and Server Compatibility:

    • Operating Systems: Runs on various platforms including Windows, Linux, Unix, macOS X, etc.
    • Web Servers: Compatible with almost all server software used today, including Apache, IIS, etc.
    • Databases: Supports a wide range of database management systems.

History and Evolution of PHP

  • Origins (1995):

    • Created by Rasmus Lerdorf under the original name PHP Tools.
    • June 8, 1995: PHP 1.0 was officially posted to the Usenet newsgroup comp.infosystems.www.authoring.cgi.
    • Rasmus Lerdorf integrated his tools with the Apache web server, and less than a year later, PHP 2.0 was posted to the same Usenet group.
  • PHP/FI (April 1996):

    • Known as PHP/FI (Forms Interpreter), granted version 2.0 status.
    • Included built-in support for DBM, mSQL, and Postgres95 databases, HTTP cookies, and user-defined functions.
  • PHP 3.0:

    • Zeev Suraski and Andi Gutmans rewrote the core parser, establishing the recursive name PHP: Hypertext Preprocessor.
    • Introduced a mature interface for multiple databases, protocols, and APIs, as well as object-oriented programming support and a consistent language syntax.
  • PHP 4.0 (May 2000):

    • Powered by the newly created Zend Engine.
    • Added support for various web servers, HTTP sessions, output buffering, secure user input handling, and new language constructs.
  • PHP 5.0 (July 2004):

    • Driven by the Zend Engine 2.0.
    • Introduced a brand new object model along with dozens of advanced language features.
  • PHP 7.0 (December 2015):

    • Development code name: phpng (PHP Next Generation).
    • Engine: Zend Engine 3.
    • Performance: Significantly faster execution speeds compared to PHP 5.6.
    • New Features: Improved error handling, stricter type declarations for function arguments, and new operators such as the spaceship operator (<=>, also known as the three-way comparison operator).
  • PHP 8.0 (November 26, 2020):

    • Major release bringing significant language improvements.
    • Key Feature: Introduced Just-In-Time (JIT) compilation, providing substantial performance gains.
  • PHP 8.2.8 (July 4, 2023):

    • Released as a stable update in the PHP 8 release series.

XAMPP Installation Guide

  • Step 1: Download XAMPP

    • Visit the official XAMPP website at https://www.apachefriends.org/.
    • Choose your operating system (Windows, macOS, or Linux).
    • Download the installer matching your system architecture (32-bit or 64-bit).
  • Step 2: Install XAMPP

    • Locate the downloaded executable file (.exe) and double-click to start installation.
    • If prompted by User Account Control (UAC), click Yes to allow administrative privileges.
  • Step 3: Setup Wizard Configuration

    • Welcome Screen: Click Next.
    • Select Components: Select required server components (Apache, MySQL, PHP, and phpMyAdmin are selected by default for a standard setup). Click Next.
    • Installation Folder: Select destination directory (default location is C:\xampp). Click Next.
    • Start Menu Folder: Choose the Start Menu folder name. Click Next.
    • Ready to Install: Click Install to execute software extraction and setup.
  • Step 4: Complete Installation

    • Click Finish to complete the setup process.

PHP Architecture and File Structure

PHP execution request-response process diagram

  • File Extensions and Composition:

    • PHP files use the file extension .php.
    • A PHP file is a text document containing plain text, HTML, CSS, JavaScript, and PHP code blocks interspersed within the HTML markup.
  • Server-Side Execution Workflow:

    1. The Client Browser issues an HTTP Page Request to the Web Server.
    2. The Web Server detects the .php extension and passes the PHP Code to the PHP Parser.
    3. The PHP Parser executes the scripting statements, handles database or file operations, and generates output as plain HTML.
    4. The PHP Parser returns the rendered HTML back to the Web Server.
    5. The Web Server transmits the plain HTML response back to the Client Browser for display.

Basic Syntax Rules

  • Canonical PHP Tags:
    • PHP scripts begin with <?php and end with ?>.
    • Code blocks can be embedded anywhere within an HTML document.
  <?php
  // PHP statements go here
  ?>
&nbsp;&nbsp;```

- **Statement Termination:**
  - Syntax rules are derived from C and C++ languages.
  - Every individual PHP statement or expression must end with a semicolon `;`.

php Hello World!"; ?>   ```

  • Case Sensitivity Rules:

    • Keywords, Functions, and Classes: Are NOT case-sensitive. Keywords such as if, else, while, echo, user-defined functions, and classes execute identically regardless of capitalization.
    <!DOCTYPE html>
    <html>
    <body>
    <?php
    ECHO "Hello World!<br>";
    echo "Hello World!<br>";
    EcHo "Hello World!<br>";
    ?>
    </body>
    </html>
    &nbsp;&nbsp;&nbsp;&nbsp;```
    
    - **Variables:** Variable names **ARE** case-sensitive. For example, `$color`, `$COLOR`, and `$coLOR` represent three completely distinct variables.
    

    php "; // Outputs "red" echo "My house is " . $COLOR . "
    "; // Triggers undefined variable notification echo "My boat is " . $coLOR . "
    "; // Triggers undefined variable notification ?>     ```

  • Comments in PHP:

    • Comments are non-executable text blocks designed for code documentation, explaining logic to developers, or temporarily disabling code during testing.
    • Single-line comments: Defined using double slashes // or a hash # symbol.
    • Multi-line comments: Enclosed within /* and */ delimiters.
  <?php
  // This is a single-line comment

  # This is also a single-line comment

  /* This is a
     multi-line comment */
  ?>
&nbsp;&nbsp;```

# PHP Variables

- **Key Rules and Characteristics:**
  - Variables store data types including text strings, integers, floating-point numbers, arrays, and objects.
  - **No Explicit Declaration:** PHP does **NOT** require explicit declaration of variable type before assigning a value to it.
  - **Automatic Data Type Conversion:** PHP automatically loosely binds and converts the variable to its correct data type based on the assigned value.
  - **Reusability:** Once declared, variables can be referenced and modified throughout the code.
  - **Assignment Operator:** The equals sign `=` operator assigns values to variables.

- **Variable Declaration Syntax:**
  - Syntax: `$variable_name = value;`

php "; echo "Age: $age"; ?>   ```

  • Variable Naming Conventions:
    • Must start with a dollar sign $, followed directly by the variable name.
    • The first character after the $ sign must be a letter (A-Z, a-z) or an underscore _.
    • Variable names CANNOT start with a number.
    • Variable names can only contain alphanumeric characters and underscores (A-z, 0-9, and _).
    • Variable names CANNOT contain spaces.
    • Variable names are strictly case-sensitive.

Echo and Print Output Statements

  • Comparison Between echo and print:

    • echo:
    • Has no return value.
    • Can accept multiple parameters separated by commas (though multi-parameter usage is rare).
    • Marginally faster in performance than print because no return evaluation takes place.
    • print:
    • Has a return value of 1, allowing it to be evaluated inside expressions.
    • Can take only one string argument.
    • Can be called with or without parentheses (print or print()).
  • Code Examples:

    • Displaying Strings of Text with print:
    <?php
    // Displaying string of text
    print "Hello, World!";
    ?>
    &nbsp;&nbsp;&nbsp;&nbsp;```
    
    - **Displaying HTML Code and Inline Styles with `print`:**
    

    php This is a simple heading."; print "

    This is heading with style.
    "; ?>     ```


    • Displaying Variables and Array Elements with echo:
    <?php
    // Defining variables
    $txt = "Hello World!";
    $num = 123456789;
    $colors = array("Red", "Green", "Blue");
    
    // Displaying variables
    echo $txt;
    echo "<br>";
    echo $num;
    echo "<br>";
    echo $colors[0];
    ?>
    &nbsp;&nbsp;&nbsp;&nbsp;```
    
    - **Browser Output for Above Script:**
    

    Hello World! 123456789 Red     ```