Show Menu
Cheatography

PHP & Symfony Cheat Sheet (DRAFT) by

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

false
(boolean)
Everything else
0
(integer)
0.0
&
-0.0
(float)
"­"
(empty string) &
"­0"
(string zero)
[]
(empty array)
null
& unset variables

Operators

 
?:
$var = expr1 ? expr2 : expr3;
if (expr1 = true) {

    $var = expr2

else

    $var = expr3

}
$var = expr1 ?: expr2;
$var = expr1 ? expr1 : expr2;
??
$var = 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 annota­tions on a class or a method
/** 
* @Suppr­ess­War­nin­gs(­PHP­MD.L­on­gVa­riable)
* @Suppr­ess­War­nin­gs(­PHP­MD.U­nu­sed­Loc­alV­ari­able)
*/

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 expres­sions. 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',
};
 

Serializer wording

Run a command

run a generic symfony command :
php bin/co­nsole app:test
run a command giving PHP more memory
php -d memory­_li­mit=2G bin/co­nsole ...

DB related commands

Make a migration
php bin/co­nsole make:m­igr­ation
Migrate (apply migrat­ions)
php bin/co­nsole doctri­ne:­mig­rat­ion­s:m­igrate
Apply only a specific migration (dry-run)
php bin/co­nsole doctri­ne:­mig­rat­ion­s:e­xecute --dry-run 202405­021­13013
Apply only a specific migration
php bin/co­nsole doctri­ne:­mig­rat­ion­s:e­xecute 202405­021­13013
Undo a specific migration
php bin/co­nsole doctri­ne:­mig­rat­ion­s:e­xecute --down 202405­021­13013
Run a mySQL command
php bin/co­nsole doctri­ne:­que­ry:sql "­SELECT * FROM table"