PHP Programming — Full Course
From your first script to namespaces, traits, and a working API — the language behind WordPress itself.
From your first script to namespaces, traits, and a working API — the language behind WordPress itself.
PHP was created by Rasmus Lerdorf in 1994, originally as a small set of tools for tracking visits to his personal homepage (the name literally started as “Personal Home Page”). Today it powers a huge share of the entire web — including WordPress, which alone runs on roughly 40% of all websites, and by extension, […]
Every PHP variable starts with a dollar sign, and — like Python and JavaScript, unlike Java or C++ — you never declare its type up front. PHP figures the type out automatically from whatever value you assign. $age = 25; // intn$price = 19.99; // floatn$name = “Tutoline”; // stringn$isActive = true; // bool Core […]
Comparison: == vs === 0 == “0” // true (converts types first)n0 === “0” // false (checks type too — prefer this)n”5″ == 5 // truen”5″ === 5 // false Exactly like JavaScript, always prefer === and !== over == and != in PHP. PHP’s automatic type conversion in loose comparisons has historically caused genuine, […]
function greet($name, $greeting = “Hello”) {n return “$greeting, $name!”;n}nnecho greet(“Yash”); // Hello, Yash!necho greet(“Yash”, “Welcome”); // Welcome, Yash! Type declarations (recommended) function greet(string $name, string $greeting = “Hello”): string {n return “$greeting, $name!”;n} Modern PHP supports optional type declarations on parameters and return values — string $name and : string above. These are not required, […]
PHP gives you built-in “superglobal” arrays for reading request data — this is how a script finds out what a visitor submitted through a form, or what parameters were included in the URL. <form method=”post” action=”submit.php”>n <input type=”text” name=”username”>n <button type=”submit”>Send</button>n</form> // inside submit.phpn$username = $_POST[“username”] ?? “”;necho “Welcome, ” . htmlspecialchars($username); Always sanitize and […]
class Course {n public string $title;n private int $lessonCount;nn public function __construct(string $title, int $lessonCount) {n $this->title = $title;n $this->lessonCount = $lessonCount;n }nn public function getTitle(): string {n return $this->title;n }n}nn$phpCourse = new Course(“PHP”, 10);necho $phpCourse->getTitle(); // PHP __construct and $this __construct runs automatically when you create a new object with new — the same […]
PDO — PHP’s database toolkit $pdo = new PDO(“mysql:host=localhost;dbname=tutoline”, “user”, “password”);nn$stmt = $pdo->prepare(“SELECT name, score FROM students WHERE course = :course”);n$stmt->execute([“course” => “PHP”]);nnforeach ($stmt->fetchAll() as $row) {n echo $row[“name”] . “: ” . $row[“score”] . “n”;n} PDO (PHP Data Objects) is a database abstraction layer — the same PDO code works against MySQL, PostgreSQL, SQLite, […]
As a project grows past a handful of files, two problems reliably show up: class names collide with each other, and you end up hand-writing dozens of require statements just to make everything available. Namespaces and Composer solve both problems together. Namespaces namespace TutolineCourses;nnclass Course {n // …n}nn// elsewhere:nuse TutolineCoursesCourse;n$course = new Course(); A namespace […]
Traits — reusable method sets trait Loggable {n public function log(string $message): void {n echo “[” . static::class . “] $messagen”;n }n}nnclass Course {n use Loggable;n}nn$course = new Course();n$course->log(“Created”); // [Course] Created PHP classes can only extends one parent, but they can use multiple traits at once. A trait shares real method implementations across otherwise […]