PHP Cheatsheet
Data and Tooling
Use this PHP reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
JSON
$data = json_decode($json, true, flags: JSON_THROW_ON_ERROR); $json = json_encode($data, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
Pass true to decode into associative arrays. Use JSON_THROW_ON_ERROR to avoid silent null failures.
Files
$text = file_get_contents('input.txt'); file_put_contents('out.txt', $text); file_put_contents('app.log', $line, FILE_APPEND); $handle = fopen('big.txt', 'r'); while (($line = fgets($handle)) !== false) {} fclose($handle);
Use simple helpers for small files and stream handles for large files.
CSV
$h = fopen('data.csv', 'r'); $header = fgetcsv($h); while (($row = fgetcsv($h)) !== false) { $record = array_combine($header, $row); } fclose($h); $out = fopen('report.csv', 'w'); fputcsv($out, ['name', 'score']); fputcsv($out, ['Ada', 95]); fclose($out);
fgetcsv handles quoted fields and embedded commas correctly, so never explode(',') real CSV.
PDO Setup and Queries
$pdo = new PDO($dsn, $user, $pass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ]); $stmt = $pdo->prepare('select * from users where email = :email'); $stmt->execute(['email' => $email]); $row = $stmt->fetch(); // one row or false $rows = $stmt->fetchAll(); // all rows $stmt = $pdo->prepare('select id from users where role = ?'); $stmt->execute([$role]); $ids = $stmt->fetchAll(PDO::FETCH_COLUMN);
Prepared statements keep values separate from SQL code and help prevent injection. Never interpolate user input into SQL strings.
PDO Insert, Update, Transactions
$stmt = $pdo->prepare('insert into users (email, role) values (:email, :role)'); $stmt->execute(['email' => $email, 'role' => 'student']); $id = $pdo->lastInsertId(); $stmt = $pdo->prepare('update users set role = ? where id = ?'); $stmt->execute(['admin', $id]); $stmt->rowCount(); // affected rows $pdo->beginTransaction(); try { $pdo->prepare('update accounts set balance = balance - ? where id = ?')->execute([$amt, $from]); $pdo->prepare('update accounts set balance = balance + ? where id = ?')->execute([$amt, $to]); $pdo->commit(); } catch (Throwable $e) { $pdo->rollBack(); throw $e; }
Wrap multi-statement changes in a transaction so they succeed or fail as a unit.
Composer
composer init composer require guzzlehttp/guzzle composer require --dev phpunit/phpunit composer dump-autoload
Load dependencies with require __DIR__ . '/vendor/autoload.php';.
Testing and Static Analysis
vendor/bin/phpunit vendor/bin/pest vendor/bin/phpstan analyse vendor/bin/psalm vendor/bin/php-cs-fixer fix
Tests prove behavior. Static analysis catches type and shape mistakes before runtime.
Environment Configuration
$databaseUrl = getenv('DATABASE_URL'); $debug = ($_ENV['APP_DEBUG'] ?? 'false') === 'true';
Keep secrets in environment variables or a secret manager, not committed source files.