Complete PHP Course: Beginner to Advanced
This PDF contains a complete self-study course covering PHP basics, Object-Oriented PHP, form
handling (frontend and backend), and MySQL connection and operations with practical examples
and explanations.
1. Introduction
PHP is a server-side scripting language designed for web development. It can be embedded into
HTML to add dynamic features to websites.
Installation:
- Windows: Use XAMPP or WAMP.
- Mac: Use MAMP.
- Linux: Use LAMP.
Running PHP scripts:
- Through web server ([Link]
- Command Line Interface (CLI): php [Link]
2. PHP Basics
Syntax: PHP code is written between <?php and ?> tags.
Variables: Start with $ sign. Example: $name = "John";
Data types: String, Integer, Float, Boolean, Array, Object.
Operators: +, -, *, /, %, ==, ===, !=, &&, ||, !
Control Structures:
- if, elseif, else
- switch-case
Loops:
- while
- do-while
- for
- foreach
Functions:
function greet($name) {
return "Hello, $name";
echo greet("John");
3. Working with Forms
HTML Form example:
<form method="POST" action="[Link]">
Name: <input type="text" name="name">
<input type="submit">
</form>
In [Link]:
$name = $_POST['name'];
Input validation: check required fields.
Sanitization: htmlspecialchars(), filter_var().
File uploads: Use <input type="file"> and move_uploaded_file().
Security:
- CSRF: Use tokens.
- XSS: Escape output.
4. PHP & MySQL
Connecting using MySQLi:
$conn = new mysqli("localhost", "root", "", "test");
INSERT:
$sql = "INSERT INTO users (name) VALUES ('John')";
$conn->query($sql);
SELECT:
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo $row['name'];
Prepared Statements:
$stmt = $conn->prepare("INSERT INTO users (name) VALUES (?)");
$stmt->bind_param("s", $name);
$stmt->execute();
5. Advanced PHP (OOP)
Classes & Objects:
class Car {
public $color;
function setColor($c) {
$this->color = $c;
$car1 = new Car();
$car1->setColor('red');
Constructors:
class Car {
function __construct() {
echo "Car created";
Inheritance:
class Vehicle {}
class Car extends Vehicle {}
Interfaces:
interface Driveable { public function drive(); }
class Car implements Driveable {
public function drive() { echo "Driving"; }
}
Exceptions:
try {
throw new Exception("Error");
} catch (Exception $e) {
echo $e->getMessage();
}
6. Real-world Project: Web Form + Database
Registration form:
<form method="POST" action="[Link]">
Username: <input name="username">
Password: <input type="password" name="password">
<input type="submit">
</form>
[Link]:
$conn = new mysqli(...);
$username = $_POST['username'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
Login: Check username, verify password with password_verify().
Sessions: session_start(), $_SESSION.
Security: Escape inputs, use prepared statements.
7. Extras
Send email:
mail($to, $subject, $message);
File handling:
$file = fopen("[Link]", "w");
fwrite($file, "Hello");
fclose($file);
Composer:
composer init
composer require vendor/package
[Link]
8. Complete Example Project
- Register new users
- Login/logout with session
- Profile page shows user info
- Admin CRUD: list users, edit, delete
Use MySQLi or PDO for DB access. Secure all input. Use sessions for auth.
9. Deployment
Host your PHP site:
- Buy domain and hosting (with PHP support)
- Upload files via FTP
- Export MySQL database, import on hosting
- Update database config in your PHP files
Test site online.