PHP Beginner
Server-side scripting language — built for the web, driving dynamic template generation, robust OOP frameworks, and secure APIs.
Table of Contents
Variables & Output Tags
PHP execution begins inside the <?php tag boundary. Variables are prefixed with a $ character, dynamically typed, and statements require ending semicolons.
<?php // Single-line comment /* Multi-line comment block */ $domain = "vidux.sh"; $status = "Active"; // String concatenation uses the dot (.) operator echo "System target: " . $domain; // Double quotes parse variables natively inside the text echo "<p>Status metrics: {$status}</p>";
Core Types & Array Architectures
PHP fields encompass standard primitive metrics and complex multi-purpose arrays that can be structured as both indexed vectors and explicit associative dictionaries.
Conditionals & Logical Blocks
Control processes via if-elseif-else or strict structures. Use identical evaluation symbols (===) to validate both content values and type matching.
$responseCode = 200; if ($responseCode === 200) { echo "Operational success."; } elseif ($responseCode >= 400 && $responseCode < 500) { echo "Client execution block mismatch error."; } else { echo "Generic terminal fallback triggered."; }
Loops & Foreach Keymaps
While standard for bounds execute predictably, foreach provides the cleanest interface for unpacking structural associative mappings safely.
$logConfig = [ "app_name" => "testName", "version" => "1.0.4", "env" => "production" ]; // Unpacking associative keys and matching values directly foreach ($logConfig as $parameter => $setting) { echo "Setting up: {$parameter} assigned to {$setting}\n"; }
Functions & Type Hinting
Modern PHP strictly utilizes explicit scalar data type rules for parameters alongside enforced return signature mapping blocks.
declare(strict_types=1); // Enforces strict type compliance matching function generateTransactionId(string $prefix, int $entropy): string { $uniqueHash = md5((string)time() . $entropy); return $prefix . "_" . substr($uniqueHash, 0, 8); } $txId = generateTransactionId("TX", 9942);
Classes & Object Methods
PHP implements an Object-Oriented model featuring explicit visibility attributes (public, protected, private) alongside standard class definitions.
class DatabaseConfig { private string $dsn; // PHP 8+ Constructor Property Promotion architectural shorthand public function __construct(private string $host, private int $port) { $this->dsn = "mysql:host=" . $this->host . ";port=" . $this->port; } public function getConnectionString(): string { return $this->dsn; } } $configInstance = new DatabaseConfig("127.0.0.1", 3306);
$this->property). Dropping the pointer format results in structural execution parsing failures.
Superglobals ($_GET, $_POST)
Superglobals are built-in arrays accessible everywhere regardless of scope. They contain incoming runtime client vectors like query strings or multi-part forms.
// Extracting safe attributes filtering raw input vectors manually $userEmail = $_POST['email'] ?? null; if ($userEmail !== null) { // Filter and sanitize string inputs securely before operating data $cleanEmail = filter_var($userEmail, FILTER_SANITIZE_EMAIL); if (filter_var($cleanEmail, FILTER_VALIDATE_EMAIL)) { echo "Validated secure email parameter: " . htmlspecialchars($cleanEmail); } }
Database Interactivity via PDO
PHP Data Objects (PDO) deliver a secure database abstraction layer. Always leverage **Prepared Statements** to entirely prevent SQL Injection exploits.
$pdoConnection = new PDO('mysql:host=localhost;dbname=testName', 'root', 'secret_pass', [ PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ]); // Structuring parameterized placeholders $queryStatement = $pdoConnection->prepare("SELECT id, username FROM users WHERE status = :status"); $queryStatement->execute(['status' => 'active']); $allActiveUsers = $queryStatement->fetchAll();