PHP Cheat Sheet
Practical PHP cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.
php syntax examples reference
PHP cheat sheet with real commands and snippets for setup, core workflows, debugging, and production-safe automation patterns. If you are working across tools, pair this with the Nginx Cheat Sheet and Regex Cheat Sheet.
Setup and Tooling
Goal: Install and verify language toolchain
# Run php command
php --version
# Run composer command
composer --version
# Run composer command
composer init
Core Workflows
Goal: Run and build source code
# Execute php workflow
php index.php
# Execute php workflow
php -S localhost:8000 -t public
Goal: Use a practical code snippet
<?php
declare(strict_types=1);
function greet(string $name): string {
return "Hello, {$name}!";
}
echo greet('Dev');
Testing and Formatting
Goal: Run automated tests
# Execute test-related command
vendor/bin/phpunit
# Execute test-related command
vendor/bin/pest
Goal: Run lint and formatter checks
# Execute quality command
vendor/bin/php-cs-fixer fix
# Execute quality command
vendor/bin/phpstan analyse
Automation and CI
Goal: Create a repeatable CI shell step
# Run strict shell mode for CI step
set -euo pipefail
# Install dependencies deterministically
npm ci || pip install -r requirements.txt || true
# Run tests and fail fast on errors
vendor/bin/phpunit
Debugging and Troubleshooting
Goal: Trace runtime issues
# Print runtime and package manager versions
node --version || python --version || true
# Search stack traces and TODO markers
rg "Traceback|Exception|TODO" .
# Re-run tests with verbose output
vendor/bin/phpunit -v || vendor/bin/phpunit --verbose || true
Common Gotchas
- Pin PHP language/runtime versions in local and CI environments.
- Keep formatting and linting automated to avoid noisy diffs in code review.
- Prefer explicit dependency lock files for reproducible builds.
- Write small, deterministic tests that can run quickly in pre-commit checks.
- Document compiler/interpreter flags used in production builds.
Related Sheets
- Nginx Cheat Sheet — daily Nginx commands and production-ready examples.
- Regex Cheat Sheet — daily Regex commands and production-ready examples.
- Ruby Cheat Sheet — daily Ruby commands and production-ready examples.
Related Cheat Sheets
Nginx Cheat Sheet
Practical Nginx cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.
Regex Cheat Sheet
Practical Regex cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.
Ruby Cheat Sheet
Practical Ruby cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.