Show Menu

PHP Essentials Cheat Sheet by

php

Language basics

script start
<?php
script end
?> or nothing
command termin­ation
;
inline comment
// simple oneliner
block comment
/* multiline text */
run script from file (if error throws warning)
inclu­de(­'sc­rip­t.p­hp');
run script from file (errors are fatal)
requi­re(­'sc­rip­t.p­hp');

Variables

declar­ation without value
$vari­abl­eName;
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

addition
$int1 + $int2 -> 12
divison
6 / 3 -> 2
multip­lic­ation
6 * 6 -> 36
expone­nti­ation
2 ** 4 -> 16
substr­action
2 - 1 -> 1
string + int (string get converted) #1
"13­shi­ts" + 13 -> 26
string + int (string get converted) #1
"sh­its­of1­3" + 13 -> 13
string + string
"2d­ogs­" + "­5do­gs" -> 7
strings concat
$str1 . $str2 -> hello stranger!
string and int concat
"numero uno - " . 1; -> numero uno - 1
unset - delete variable
unset­($v­ari­able);
$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
$arra­y[i­ndex]
push data to end of array
array­_pu­sh(­$array, $data); OR $array[] = $data;
push data to spec­ific index of array
$arra­y['­myC­olor'] = "­#FF­F";
remove & get last element of array
array­_po­p($­arr­ay);
get count of elements in array
count­($a­rray);
is specific value in array?
in_ar­ray­($n­eedle, $array);
 

Output

simple print
echo(­$va­riable . " and some text");
simple print #2
echo 1;
simple print #3 (must be without parent­heses)
echo 1,2; -> 12
print multiple lines
echo(­"­lin­e1­\nli­ne2­\n");
print variable type and data
print­_r(­$var);
print variable type and data (detailed)
var_d­ump­($v­ar);
condit­ional printing
echo(­$var1 == 1 ? "its 1!" : "its not 1!");

Loops

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;
}
fore­ach - do something for each element in array
forea­ch(­$array as $arrayElement){
var_dump($arrayElement;)
}
while - do something if cond is true again and again if is still true
while(true){
echo("infinity!");
}
break - escape from loop
break;
cont­inue - skip current round
conti­nue;

PHP Filesystem Functions

check if file exists
file_­exi­sts­("/r­oot­/fi­le.t­xt­");
open a file for reading only
$file = fopen(­"./../f­ile.tx­t", "­r");
get size of file in bytes
files­ize­("fi­le.t­xt­");
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
fwrit­e($­file, "some text\n­");
close file after all operations
fclos­e($­file);
oneline file write
file_­put­_co­nte­nts­($f­ile­name, $data);
oneline file read
$file­Content = 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
Server and execution enviro­­nment inform­­ation
$_SE­R­VE­­R['­­SE­R­V­ER­­_AD­­DR']
Server IP
$_SE­R­VE­­R['­­RE­Q­U­ES­­T_M­­ET­H­OD']
Which request method was used to access the page; i.e. 'GET', 'HEAD', 'POST', 'PUT'
$_SE­R­VE­­R['­­HT­T­P­_U­­SER­­_A­G­ENT']
User-A­­gent: header from the current request
$_SE­R­VE­­R['­­RE­M­O­TE­­_AD­­DR']
The IP address from which the user is viewing the current page
__FILE__
The full path and filename of the current script file
__DIR__
The directory of the file
__LINE__
Current line number in script file
$_GET
Associ­ative array of variables passed to the current script via the URL parameters
$_POST
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
$_FILES
An associ­ative array of items uploaded to the current script via the HTTP POST method

Download the PHP Essentials Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/davidsykora_php-essentials.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

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
          MySQL Cheat Sheet
          PHP-FIG PSR Standards Cheat Sheet

          More Cheat Sheets by davidsykora