PHP Cheatsheet
Frameworks and Deployment
Use this PHP reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
PSR Standards
| Standard | What it covers |
|---|---|
| PSR-4 | Autoloading class names to files |
| PSR-7 | HTTP message interfaces |
| PSR-11 | Dependency container interface |
| PSR-12 | Coding style |
| PSR-15 | HTTP middleware |
| PSR-18 | HTTP client interface |
Knowing the PSRs helps when moving between Laravel, Symfony, Slim, Mezzio, and library code.
Laravel Routing and Validation
Route::get('/users/{id}', [UserController::class, 'show']); Route::post('/users', [UserController::class, 'store']); class UserController { public function show(int $id): JsonResponse { return response()->json(User::findOrFail($id)); } public function store(Request $request): JsonResponse { $validated = $request->validate([ 'email' => ['required', 'email', 'unique:users'], 'name' => ['required', 'string', 'max:100'], ]); return response()->json(User::create($validated), 201); } }
validate() returns only the validated fields and automatically responds 422 with per-field errors when validation fails.
Laravel Eloquent Queries
User::find($id); // null if missing User::findOrFail($id); // 404 via exception User::where('role', 'admin')->orderBy('name')->limit(10)->get(); User::where('created_at', '>=', now()->subDays(7))->count(); User::firstOrCreate(['email' => $email], ['name' => $name]); $user->update(['name' => 'Ada']); $user->posts()->latest()->get(); // relationship query User::with('posts')->get(); // eager load, avoids N+1
Laravel Migrations
Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('email')->unique(); $table->string('name'); $table->foreignId('team_id')->constrained()->cascadeOnDelete(); $table->timestamps(); });
php artisan make:migration create_users_table php artisan migrate php artisan migrate:rollback
Symfony Controllers and Validation
#[Route('/users/{id}', methods: ['GET'])] public function show(int $id, UserRepository $users): JsonResponse { return $this->json($users->find($id)); } final class CreateUserInput { #[Assert\NotBlank] #[Assert\Email] public string $email; } #[Route('/users', methods: ['POST'])] public function store(#[MapRequestPayload] CreateUserInput $input): JsonResponse { // $input is deserialized and validated, invalid payloads get 422 return $this->json(['email' => $input->email], 201); }
Symfony emphasizes components, dependency injection, attributes, and explicit services.
Doctrine Queries
$users->find($id); $users->findOneBy(['email' => $email]); $users->findBy(['role' => 'admin'], ['name' => 'ASC'], 10); $qb = $users->createQueryBuilder('u') ->where('u.createdAt >= :since') ->setParameter('since', $since) ->orderBy('u.name', 'ASC'); $recent = $qb->getQuery()->getResult(); $em->persist($user); $em->flush();
php bin/console make:migration php bin/console doctrine:migrations:migrate
Dependency Injection
final class BillingService { public function __construct(private PaymentGateway $gateway) {} }
Inject dependencies instead of creating them inside business logic. This improves testability and lets framework containers wire implementations.
PHP-FPM and Web Servers
Production PHP usually runs behind Nginx or Apache, which forwards PHP requests to PHP-FPM.
browser -> nginx/apache -> php-fpm worker -> app code -> response
Static files should be served by the web server or CDN, not by PHP application code.
OPcache
OPcache keeps compiled PHP bytecode in memory.
opcache.enable=1 opcache.validate_timestamps=0 opcache.memory_consumption=128
In production, pair OPcache settings with a deployment process that restarts or reloads PHP-FPM after new code ships.
Configuration Checklist
- Set
APP_ENV=prodand disable debug output in production. - Keep secrets outside Git and rotate them when exposed.
- Log errors server-side; show generic messages to users.
- Use HTTPS and secure cookies for sessions.
- Run migrations as an explicit deploy step.
- Cache config/routes/views only when the deploy can refresh them.
Debugging Production Issues
error_log('payment failed: ' . $e->getMessage());
Use structured application logs when possible. Avoid var_dump or stack traces in public responses.