A PHP framework is a pre-built structure for the parts every web application needs: routing, database access, templating, sessions, validation and security.
You can write all of it yourself. A framework means you do not have to, and that someone else has already made the security mistakes.
What you get#
| Feature | Without a framework | With one |
|---|---|---|
| Routing | A file per URL, or hand-written parsing | Route::get("/users/{id}", ...) |
| Database | Write every SQL query | An ORM, with placeholders enforced |
| Templates | PHP mixed into HTML | A template engine that escapes by default |
| Security | CSRF tokens and escaping by hand | Built in |
| Structure | Whatever you invent | A convention others recognise |
The last row matters more than people expect. A conventional structure means a new developer can find things, and so can you in six months.
MVC in one paragraph#
Most PHP frameworks follow Model-View-Controller. The model holds data and business rules. The view is the template that produces HTML. The controller receives the request, asks the model for what it needs, and hands it to a view. Keeping those three apart is what stops a project turning into 2,000-line files of mixed SQL and HTML.
The main options#
Laravel#
The most popular by a distance. Batteries included: authentication, queues, migrations, mail, an ORM and a template engine all present from the start. Excellent documentation and a very large community, which means most problems you hit have already been answered.
Good for: most web applications, and beginners who want the path well-marked.
Trade-off: a lot of magic. Things work without you seeing how, which is comfortable until you need to debug it.
Symfony#
More explicit and more configurable. Built from independent components that other projects — Laravel included — reuse. Enterprise codebases favour it.
Good for: large, long-lived applications and teams that want everything visible.
Trade-off: steeper start, more configuration before the first page appears.
CodeIgniter#
Small, fast, and light on configuration. Runs comfortably on modest shared hosting.
Good for: smaller projects, and learning MVC without much ceremony.
Trade-off: a smaller ecosystem, so more things you build yourself.
Slim#
A micro-framework: routing and middleware, essentially nothing else. You choose your own database layer and templating.
Good for: APIs and small services.
Trade-off: you assemble the rest of the stack yourself.
What framework code looks like#
<?php
// Routing
Route::get("/users/{id}", [UserController::class, "show"]);
// Controller
class UserController {
public function show(int $id) {
$user = User::findOrFail($id);
return view("users.show", ["user" => $user]);
}
}
?><!-- Template: escaped automatically -->
<h1>{{ $user->name }}</h1>Compare that with the plain-PHP equivalent: a file per URL, a hand-written query with placeholders, and htmlspecialchars() on every output you must remember to add.
Composer#
Every modern PHP framework is installed with Composer, PHP’s package manager:
composer create-project laravel/laravel my-app
cd my-app
php artisan serveComposer also gives you autoloading, so you stop writing require for every class. Worth learning even if you never use a framework.
When plain PHP is right#
- A handful of pages with a contact form
- A single script that does one job
- Adding to an existing plain-PHP codebase
- You are learning, and want to see the mechanics
A framework brings a folder structure, a dependency to keep updated and a body of conventions to learn. For a three-page site that is a cost with no return.
Questions people ask#
Which framework should I learn first?
Laravel, on weight of jobs, tutorials and community answers. Symfony if you already know you are heading into enterprise work.
Is PHP still worth learning?
It runs a large share of the web, including WordPress, and modern PHP 8 is a considerably better language than its reputation suggests. Plenty of work exists.
Do I need to know OOP first?
Yes. Every framework is built from classes, interfaces and dependency injection. See OOP in PHP.
What is an ORM?
A layer that maps database rows to objects, so you write User::find(1) instead of SQL. Convenient — and learn SQL first, so you can tell what it is generating when something is slow.