This guide will help you quickly revise PHP basics, OOP concepts, MVC, Composer, and modern best practices.
PHP Basics Refresher
πΉ Data Types in PHP
PHP supports 8 main data types:
- String β
"Hello" - Integer β
10 - Float/Double β
10.5 - Boolean β
true/false - Array β
[1, 2, 3] - Object β instance of a class
- NULL β no value
- Resource β special types (DB connection, file handles)
πΉ Loops in PHP
- for β fixed iterations
- while β runs while condition is true
- do…while β runs at least once, then checks condition
- foreach β loops arrays/objects
πΉ NULL vs Empty
- NULL β no value at all.
- Empty β value exists but is considered empty (
"",0,false,[],null).
π Allnullare empty, but not all empty are null.
πΉ Arrays
- Indexed array β numeric keys
- Associative array β string keys
- Multidimensional array β arrays within arrays
πΉ Type Casting vs Type Juggling
- Type Casting β manual conversion:
(int)"10" - Type Juggling β PHP auto-converts when needed:
"10" + 5 = 15
β οΈ Juggling can be risky. Protect against it by:
- Using strict comparison
=== - Enabling strict typing:
declare(strict_types=1);
πΉ Error Control Operator
@suppresses errors:$file = @file("missing.txt");- β οΈ Bad practice β instead use
try...catch.
Writing Professional PHP Code
Some best practices every PHP developer should follow:
- Enable strict types
- Use try…catch for error handling
- Use Composer for packages & autoloading
- Use
require_onceinstead ofrequire - Follow PSR standards (PSR-1, PSR-4, PSR-12)
- Use namespaces
- Organize code with OOP & MVC
- Validate and sanitize input
- Use prepared statements/ORM for DB
- Store configs in
.envfiles - Use dependency injection instead of globals/singletons
Dependency Injection (DI) in PHP
- Without DI: A class creates its own dependencies.
- With DI: Dependencies are provided externally (constructor/method).
π Example:
class OrderService {
private $db;
public function __construct(Database $db) {
$this->db = $db; // injected
}
}
Benefits: loose coupling, testability, easier maintenance.
Laravel handles DI automatically via its Service Container.
PDO vs MySQLi
- MySQLi β works only with MySQL.
- PDO β works with multiple databases (MySQL, SQLite, PostgreSQL, etc.).
- Laravel uses PDO by default.
Switch vs Match
- switch β loose comparison, needs
break. - match (PHP 8+) β strict comparison, returns value, no
break.
Example:
echo match($x) {
1 => "One",
2 => "Two",
default => "Other",
};
Variadic Functions
A variadic function accepts any number of arguments using ....
function sum(...$numbers) {
return array_sum($numbers);
}
echo sum(1,2,3,4); // 10
Final Thoughts
PHP remains a solid backend choice, especially when paired with Laravel. By following PSR-12 standards, dependency injection, and Composer, you can write clean, professional, and maintainable code.
Modern PHP (with features like match, variadic functions, and strict types) is far more powerful than its old reputation suggests. Combine this with Laravelβs ecosystem, and youβre ready for both small apps and enterprise-level systems.
Letβs start with OOP principles.
There are 4 main principles of OOP (easy to remember with A PIE):
- A β Abstraction
- Show only essential details, hide complexity.
- Example: You use
car->start()without knowing how engine works.
- P β Polymorphism
- One function/method can behave differently depending on context.
- Example:
draw()works differently forCircleandSquare.
- I β Inheritance
- Child class gets properties & methods from parent class.
- Example:
Doginherits fromAnimal.
- E β Encapsulation
- Bundle data & methods together, restrict access with
public/private/protected. - Example: Bank balance canβt be changed directly, only via
deposit()method.
- Bundle data & methods together, restrict access with
π Memory Trick:
Think of OOP as eating βA PIEβ π° β Abstraction, Polymorphism, Inheritance, Encapsulation.
β Thatβs your revision summary blog post for today!
