Show Menu

PHP Essentials Cheat Sheet by

Language basics

script start
script end
?> or nothing
command termin­ation
inline comment
// simple oneliner
block comment
/* multiline text */
run script from file (if error throws warning)
run script from file (errors are fatal)


declar­ation without value
string declar­ation
$string = "some text";
integer declar­ation
$int = 123;
double declar­ation
$doble = 1.2
empty array declar­ation
$array = array();
array with elements declar­ation
$a = array(­"­tex­t",1­,2.3);
boolean declar­ation
$bool = true;
null declar­ation
$empty = null;

Variable operations

$int1 + $int2
-> 12
6 / 3
-> 2
6 * 6
-> 36
2 ** 4
-> 16
2 - 1
-> 1
string + int (string get converted) #1
"­13s­hit­s" + 13
-> 26
string + int (string get converted) #1
"­shi­tso­f13­" + 13
-> 13
string + string
"­2do­gs" + "­5do­gs"
-> 7
strings concat
$str1 . $str2
-> hello stranger!
string and int concat
"­numero uno - " . 1;
-> numero uno - 1
unset - delete variable
$int1 = 5;

$int2 = 7;

$str1 = "­hello ";

$str2 = " strang­er!­";

Flow control

simple condition
if (true){
echo("condition pass");
is var1 equal to var2?
if ($var1 == $var2){
echo("it is.");
} else {
echo("it ins't.")
two vars comper­ation
($var1 == $var2)
is var equal to int?
($var1 == 4)
is var lower than int?
($int < 4)
is var bigger than int?
($int > 4)
is var lower than 5 and not equal to zero
($int < 5 && $int != 0)
is string equal to string?
($str == "­dan­ger­ous­")
is num 1 or 4?
($num == 1 || $num == 4)

Array operations

get array element by index
push data to end of array
array_­pus­h($­array, $data);
$array[] = $data;
push data to specific index of array
$array­['m­yCo­lor'] = "­#FF­F";
remove & get last element of array
get count of elements in array
is specific value in array?
in_arr­ay(­$ne­edle, $array);


simple print
echo($­var­iable . " and some text");
simple print #2
echo 1;
simple print #3 (must be without parent­heses)
echo 1,2;
-> 12
print multiple lines
print variable type and data
print variable type and data (detailed)
condit­ional printing
echo($var1 == 1 ? "its 1!" : "its not 1!");


for - classic loop (start instru­ction; condition on start of each round;end instru­ction of each round )
for ($i = 1; $i <= 10; $i++) {
echo $i;
foreach - do something for each element in array
foreac­h($­array as $arrayElement){
while - do something if cond is true again and again if is still true
break - escape from loop
continue - skip current round

PHP Filesystem Functions

check if file exists
open a file for reading only
$file = fopen(­"./../f­ile.tx­t", "­r");
get size of file in bytes
print all bytes from fopened file
echo fread(­$file, filesi­ze(­"­fil­e.t­xt"));
open a file for reading only
$file = fopen(­"./../f­ile.tx­t", "­w");
write to fopened file
fwrite­($file, "some text\n­");
close file after all operations
oneline file write
file_p­ut_­con­ten­ts(­$fi­lename, $data);
oneline file read
$fileC­ontent = file_g­et_­con­ten­ts(­$fi­leN­ame);
fopen() modes (second argument)
r = Read
r+ = Read,write and prepend
w = Write, truncate
w+ = Read and write, truncate a Write, append
a+ = Read and write, append

Useful variables

Server and execution enviro­­nment inform­­ation
Server IP
Which request method was used to access the page; i.e. 'GET', 'HEAD', 'POST', 'PUT'
User-A­­gent: header from the current request
The IP address from which the user is viewing the current page
The full path and filename of the current script file
The directory of the file
Current line number in script file
Associ­ative array of variables passed to the current script via the URL parameters
An associ­ative array of variables passed to the current script via the HTTP POST method when using applic­ati­on/­x-w­ww-­for­m-u­rle­ncoded or multip­art­/fo­rm-data as the HTTP Conten­t-Type in the request
An associ­ative array of items uploaded to the current script via the HTTP POST method


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          PHP Cheat Sheet
          oAuth End Points Cheat Sheet
          MySQL Cheat Sheet

          More Cheat Sheets by davidsykora