Cheatography
https://cheatography.com
This Cheat Sheet was created with these versions in mind :
PHP 8.2
Symfony 6.3
This is a draft cheat sheet. It is a work in progress and is not finished yet.
Truthy & Falsy
|
|
|
Everything else |
|
|
""
(empty string) & "0"
(string zero) |
|
|
Operators
|
|
|
|
?: |
$var = expr1 ? expr2 : expr3;
|
if (expr1 = true) { $var = expr2 else $var = expr3 }
|
|
$var = expr1 ? expr1 : expr2;
|
|
?? |
|
$var = isset(expr1) ? expr1 : expr2 Note : isset($var) returns false when $var is not set or null |
PHPMD, PHPSTAN snippets
PHPMD Suppress Warnings Comment annotations on a class or a method |
/** * @SuppressWarnings(PHPMD.LongVariable) * @SuppressWarnings(PHPMD.UnusedLocalVariable) */
|
match() expression
The match() expression branches evaluation based on an identity check of a value. Unlike switch, it will evaluate to a value much like ternary expressions. Unlike switch, the comparison is an identity check (===) rather than a weak equality check (==). |
$food = 'cake'; $return_value = match ($food) { 'apple' => 'This food is an apple', 'bar' => 'This food is a bar', 'cake' => 'This food is a cake', };
|
|
|
Run a command
run a generic symfony command : |
php bin/console app:test
|
run a command giving PHP more memory |
php -d memory_limit=2G bin/console ...
|
DB related commands
Make a migration |
php bin/console make:migration |
Migrate (apply migrations) |
php bin/console doctrine:migrations:migrate |
Apply only a specific migration (dry-run) |
php bin/console doctrine:migrations:execute --dry-run 20240502113013 |
Apply only a specific migration |
php bin/console doctrine:migrations:execute 20240502113013 |
Undo a specific migration |
php bin/console doctrine:migrations:execute --down 20240502113013 |
Run a mySQL command |
php bin/console doctrine:query:sql "SELECT * FROM table" |
|