PHP Live Lab
Edit PHP code and click Run. This lab simulates common PHP output from echo and simple variables.
What is PHP?
PHP is a server-side scripting language used to build dynamic websites and web applications. It runs on the server and outputs HTML to the browser.
Use PHP when you need login systems, forms, CRUD operations, APIs, sessions, and database-driven content.
Setup
Create a `.php` file in your XAMPP `htdocs` folder and open it through `http://localhost/yourfile.php`.
PHP Syntax
PHP code is written inside `<?php ... ?>`. Statements usually end with semicolons.
Variables
Variables in PHP start with `$`. PHP is dynamically typed, so you do not declare data types explicitly.
Arrays
PHP supports indexed arrays, associative arrays, and multidimensional arrays.
Conditions
Use `if`, `elseif`, `else`, and `switch` to branch logic.
Loops
PHP supports `for`, `while`, `do...while`, and `foreach` loops.
Functions
Create reusable blocks with `function`, optional type hints, and return values.
Handling Forms
Use `$_GET` and `$_POST` superglobals to receive form data from users.
MySQL with PDO
Use PDO prepared statements for secure and clean database access.
OOP in PHP
PHP supports classes, objects, inheritance, interfaces, and traits.
Sessions
Sessions help store logged-in user state across multiple pages.
Security Basics
Always validate input, escape output, and use prepared statements to prevent common vulnerabilities.
Key practices: use `password_hash`, `password_verify`, `htmlspecialchars`, CSRF tokens, and strict input validation on server side.
Includes and Requires
Use `include` and `require` to reuse header, footer, config, and helper files across pages.
File Uploads
Validate file type and size before moving uploaded files to a safe directory.
JSON and APIs
Return API responses as JSON and set the right `Content-Type` header in PHP.
Authentication Basics
Use password hashing and session checks to build secure login systems.
Error Handling and Logging
Catch runtime issues and log them to files instead of showing sensitive errors to users.