DEV Community

Cover image for My beloved PHP cheat sheet

My beloved PHP cheat sheet

Eric The Coder on December 17, 2020

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

More of a style thing, but for booleans I try to give them a verb name like:

  • isActive
  • isDefined
  • hasName
  • hasColor

which I’ve found makes my code read more semantically when the variables are used in conditional statements and loops

Collapse
 
turkindm profile image
Dmitry Turkin

Oh come on, where is PHP8 features?

Collapse
 
ericchapman profile image
Eric The Coder

Good point. Will add those later today.

Collapse
 
jonrandy profile image
Jon Randy 🎖️
<?php

//Naming convention
$first_name = 'Mike'. // all lower case with underscore separators
function updateProduct() // camelCase
class ProductItem // StudlyCaps
const ACCESS_KEY = '123abc'; // all upper case with underscore separators

//output
echo 'Hello World';

//variable declaration
$name = 'Mike'; //string
$isActive = true; //boolean
$number = 25; //integer
$amount = 99.95; //float
...
Enter fullscreen mode Exit fullscreen mode

You seem to introduce a naming convention of camel case for var names, then immediately contradict it with $isActive?

Collapse
 
ericchapman profile image
Eric The Coder

Good point :-)

Collapse
 
bawa_geek profile image
Lakh Bawa

Pretty good work, I am not sure, if it's only me, I must have used "while and do-while loop" no more than 2-3 times in the last 4 years

Collapse
 
drbyte profile image
Chris Brown • Edited

Nice list. Note: In at least 2 cases you use __construc where it should be __construct

Collapse
 
ericchapman profile image
Eric The Coder

Good catch. Corrected.

Collapse
 
ellisgl profile image
Ellis

I need to benchmark it again, but ++$i is a micro optimizion you can use on your for loops. IIRC, do..while is another micro optimization you replace your while loops with, if you can guarantee at least one iteration.

Collapse
 
bobbyiliev profile image
Bobby Iliev

That is great! I recently wrote a similar PHP guide here.

Collapse
 
kosoko_daniel profile image
Oluwagbenga

Wow this is great. Weldone

Collapse
 
costincca profile image
Constantin Coșoiu

Very very useful and concise. Do you have it as a 1 or 2 pages PDF?