Skip to content

Latest commit

 

History

History
233 lines (184 loc) · 4.56 KB

File metadata and controls

233 lines (184 loc) · 4.56 KB

Summary of PHP Documentation

Overview

PHP (Hypertext Preprocessor) is a widely-used, open-source, server-side scripting language designed for web development. It is also a general-purpose language that can be embedded into HTML.


Key Features

  • Dynamic Web Content: Generates HTML dynamically on the server.
  • Database Integration: Built-in support for databases like MySQL, PostgreSQL, and SQLite.
  • Cross-Platform: Runs on various operating systems (Linux, Windows, macOS).
  • Extensive Library Support: Standard libraries and third-party frameworks enhance functionality.
  • Ease of Integration: Embeds directly into HTML and integrates with various web technologies.

Core Concepts

Syntax Basics

  • PHP Tags:
    <?php
    echo "Hello, PHP!";
    ?>
  • Comments:
    // Single-line comment
    # Another single-line comment
    /* Multi-line
       comment */

Variables

  • Declared with a $ prefix and no explicit type declaration (dynamic typing).
    $name = "PHP";
    $version = 8.0;
    $isActive = true;

Data Types

  • Primitive: string, integer, float, boolean
  • Compound: array, object, callable, iterable
  • Special: NULL, resource

Operators

  • Arithmetic: +, -, *, /, %
  • String: . (concatenation)
  • Comparison: ==, ===, !=, !==, <, >, <=, >=
  • Logical: &&, ||, !
  • Assignment: =, +=, -=, .=

Control Structures

Conditionals

if ($age > 18) {
    echo "Adult";
} elseif ($age > 12) {
    echo "Teenager";
} else {
    echo "Child";
}

Loops

  • For Loop:
    for ($i = 0; $i < 10; $i++) {
        echo $i;
    }
  • While Loop:
    while ($count < 5) {
        echo $count++;
    }
  • Foreach Loop:
    foreach ($array as $key => $value) {
        echo "$key: $value";
    }

Functions

  • Definition:
    function greet($name) {
        return "Hello, $name!";
    }
    echo greet("PHP");
  • Default Parameters:
    function greet($name = "Guest") {
        return "Hello, $name!";
    }

Arrays

  • Indexed Arrays:
    $colors = ["red", "green", "blue"];
    echo $colors[0]; // Outputs: red
  • Associative Arrays:
    $ages = ["Alice" => 25, "Bob" => 30];
    echo $ages["Alice"]; // Outputs: 25

Object-Oriented Programming (OOP)

  • Class and Object:

    class Car {
        public $brand;
        public function __construct($brand) {
            $this->brand = $brand;
        }
        public function honk() {
            return "Honk! I am a $this->brand.";
        }
    }
    $car = new Car("Toyota");
    echo $car->honk();
  • Inheritance:

    class ElectricCar extends Car {
        public $batteryLife;
        public function charge() {
            return "Charging the battery.";
        }
    }

File Handling

  • Reading a File:
    $content = file_get_contents("example.txt");
    echo $content;
  • Writing to a File:
    file_put_contents("example.txt", "Hello, PHP!");

Database Interaction

  • Using PDO (PHP Data Objects):
    try {
        $pdo = new PDO("mysql:host=localhost;dbname=testdb", "username", "password");
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $pdo->query("SELECT * FROM users");
        foreach ($stmt as $row) {
            print_r($row);
        }
    } catch (PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
    }

Error Handling

  • Use try, catch, and finally for exception handling:
    try {
        if (!file_exists("example.txt")) {
            throw new Exception("File not found");
        }
    } catch (Exception $e) {
        echo $e->getMessage();
    }

Best Practices

  • Use strict types when possible:
    declare(strict_types=1);
  • Enable error reporting during development:
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
  • Sanitize user input to prevent security vulnerabilities (e.g., SQL injection, XSS).
  • Use prepared statements for database queries.

Resources

PHP is a versatile language, especially suited for web development, with a rich ecosystem of tools and frameworks like Laravel, Symfony, and WordPress.