DEV Community

Cover image for PHP cheat sheet (updated to PHP 8.1)

PHP cheat sheet (updated to PHP 8.1)

Eric The Coder on December 06, 2021

Follow me!: Follow @EricTheCoder_ Here is my cheat sheet I created along my learning journey. If you have any recommendations (addition/subtract...
Collapse
 
anwar_nairi profile image
Anwar •

Nice one Eric! You can also add this trick to make numbers even more readable

$bankBalance = 1_000_000;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
gregorgonzalez profile image
Gregor Gonzalez •

Oh didn't know about that. Thanks 👍

Collapse
 
vinceamstoutz profile image
Vincent Amstoutz •

Oh like in the JS env ! Thanks for this trick @khalyomede ! 😊

Collapse
 
vladi160 profile image
vladi160 •

Invokable classes:

class A {
public function __invoke() {
print_r('Hello world!);
}
}
$a = new A();
($a)(); // 'Hello World'

array_reduce:

$total = array_reduce(
$arr,
function($ac, $item) { return $ac + $item; },
0
);

Destructuring arrays:

$arr = [
[1, 'name1'],
[2, 'name2'],
];

[$firstEl, $secondEl] = $arr ; // firstEl = [1, 'name1'], secondEl = [2, 'name2']
[$id, $name] = $firstEl; // $id = 1 | $name = name1

Destructuring in a loop:

`foreach ($arr as [$id, $name]) {

}`

Collapse
 
mlsofts profile image
MLSofts •

Thank you for the very interesting article, however I have two comments while going through the article:

the first in Conditionals: switch ($ color) there are two "red" cases
the second in array: // Array declaration can contain any types
$ example = ['Mike', 50.2, true, ['10', '20'];
a "]" is missing at the end

Collapse
 
ericchapman profile image
Eric The Coder •

Done. Thanks

Collapse
 
nicholas_moen profile image
arcanemachine • • Edited

Nice cheatsheet. Very helpful.

FYI, classes don't use sTuDLy cApS, they use PascalCase aka UpperCamelCase (because camel case can mean either camelCase or CamelCase, making the word completely useless... the term lowerCamelCase should be used to avoid confusion).

Collapse
 
gregorgonzalez profile image
Gregor Gonzalez •

Love to see all php8 stuff 🤩

Collapse
 
bigdan256 profile image
BigDan256 •

Great cheat sheet.

Try to use <?php in place of <? as the tag isn't enabled in recommended production or development environments:
; short_open_tag
; Default Value: On
; Development Value: Off
; Production Value: Off
When you use it and it's not enabled, either your file will error out, or your source code will leak to your output.

Try var_export, it's similar to var_dump and print_r. It outputs content in php syntax, similar to how json_encode will return content in json syntax.

Collapse
 
salehmubashar profile image
Saleh Mubashar •

wow, i must say this is quite comprehensive.
I think you could add Superglobals as well
thanks :)

Collapse
 
llbbl profile image
Logan Lindquist •

nice job Eric

Collapse
 
ridwanishaq profile image
ridwanishaq •

Very helfull, thank very much for this wonderful articles

Collapse
 
danielsalgadop profile image
Daniel Salgado Población •

Really Nice Eric.

Have you thought in making a gist of it?

thanks

Collapse
 
code_jedi profile image
Code_Jedi •

Thank you! As a PHP developer, I see myself coming back to this over the next couple of months.

Collapse
 
vinceamstoutz profile image
Vincent Amstoutz •

Very nice cheat sheet Eric ! You can also add the tag for others popular PHP frameworks like Symfony (not only Laravel) to increase your visibility 😉