PHP Programming Cheatsheet: Modern PHP 8.x, OOP, PDO & MySQL Guide

Modern PHP 8.x Handbook

PHP 8.x: Architecture, OOP & Bulletproof Web Security

PHP powers over 75% of the web including WordPress, Wikipedia, and enterprise Laravel applications. Modern PHP 8.x features JIT compilation, strict typing, match expressions, constructor property promotion, and robust PDO database layers.

Step 1: Modern PHP 8.x Breakthrough Features

<?php
declare(strict_types=1);

// 1. Constructor Property Promotion & Union Types
class Course {
    public function __construct(
        public string $title,
        public int|float $fee,
        public ?string $duration = '3 Months' // Nullable type
    ) {}
}

$dca = new Course(title: "DCA", fee: 4999); // Named Arguments

// 2. Match Expression (replaces verbose switch statements)
$statusCode = 200;
$message = match ($statusCode) {
    200, 201 => 'Success: Request Handled',
    400 => 'Bad Request: Missing Parameters',
    404 => 'Not Found: Resource does not exist',
    default => 'Internal Server Error'
};

// 3. Nullsafe Operator (?->)
$studentCity = $student?->profile?->address?->city ?? 'Unknown';

Step 2: Bulletproof PDO Database Prepared Statements

Never concatenate raw user input into SQL queries. Always utilize PDO prepared statements with bound parameters:

<?php
$dsn = "mysql:host=localhost;dbname=seo_pragati;charset=utf8mb4";
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];

try {
    $pdo = new PDO($dsn, "db_user", "db_password", $options);

    // Secure Prepared Statement: Prevents 100% of SQL Injection attacks
    $stmt = $pdo->prepare("SELECT id, name, fee FROM courses WHERE slug = :slug AND status = :status");
    $stmt->execute([
        'slug' => 'tally-prime-course',
        'status' => 'active'
    ]);
    
    $course = $stmt->fetch();
    echo $course ? "Found: " . htmlspecialchars($course['name']) : "Not found";
} catch (PDOException $e) {
    error_log($e->getMessage());
    die("Database connection failed. Please try again later.");
}

Step 3: Web Application Security Standards (OWASP)

  • Password Hashing: Always use password_hash($password, PASSWORD_BCRYPT). Never use MD5 or SHA1. Verify with password_verify($input, $hash).
  • XSS Prevention: Always escape dynamic HTML output using htmlspecialchars($var, ENT_QUOTES, 'UTF-8').
  • CSRF Protection: Generate a cryptographically secure random token in $_SESSION['csrf_token'] = bin2hex(random_bytes(32)) and validate it on every POST request.
🎁 Free Career Guidance & Demo Session

Get Free Syllabus PDF & 2-Day Live Demo Class

Connect with our certified academic counselors today. Understand module details, installment fee plans, and how students from your area are building successful careers.

Frequently Asked Questions

Why should I use PDO instead of mysqli in PHP?
PDO supports 12 different database drivers (MySQL, PostgreSQL, SQLite, etc.) through a consistent object-oriented interface, provides named parameter binding, and features robust exception-based error handling.
How do I hash and verify passwords securely in PHP?
Use password_hash($password, PASSWORD_BCRYPT) when creating accounts. When logging in, verify the entered password against the stored hash using password_verify($inputPassword, $storedHash).
What does 'declare(strict_types=1);' do?
It enforces strict type-checking for function arguments and return values in that file, ensuring PHP does not silently coerce mismatched types (e.g. passing '10' into an integer parameter throws a TypeError).

Student Reviews

"Pragati Skill Academy-তে DCA course করে আমি সরকারি চাকরির পরীক্ষায় ভালো ফল করেছি। Teachers অনেক helpful এবং practical training চমৎকার।"

Sourav Maity

"Tally Prime course আমার career completely change করে দিয়েছে। এখন আমি একটি private firm-এ Accountant হিসেবে কাজ করছি। Thank you Pragati Skill Academy!"

Priya Das

"Computer basic course থেকে শুরু করে এখন আমি Graphic Design শিখছি। সবকিছু step by step শেখানো হয়। Very good institute!"

Rahul Mondal

"ADCA course-এর syllabus অনেক comprehensive। MS Office, Tally, Web Design সব একসাথে শেখা যায়। Best computer center in this area."

Ananya Samanta

Check Batch Availability & Certificate Delivery by PIN Code

Instant lookup for all 19,000+ Indian PIN Codes, Blocks, and Villages

Speed Post Delivery Timeline 24 - 48 Hours via Speed Post
Available Training Mode Classroom Lab & Online Hybrid
Govt Recognized Certificate ISO 9001:2015 & E-Max India
Admissions open for immediate batch starting this week. Inquire on WhatsApp for PIN 721433