PHP Essentials Cheat Sheet

A Programming › PHP cheat sheet in English by davidsykora
12,694 views 3,522 downloads 6 Oct 2017 History
3.7 avg · 0 ratings

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)
includ­e('s­cri­pt.p­hp­');
run script from file (errors are fatal)
requir­e('s­cri­pt.p­hp­');

Variables

declar­ation without value
$varia­ble­Name;
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
"­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
unset(­$va­ria­ble);
$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
$array­[index]
push data to end of array
array_­pus­h($­array, $data); OR $array[] = $data;
push data to specific index of array
$array­['my­Col­or'] = "­#FF­F";
remove & get last element of array
array_­pop­($a­rray);
get count of elements in array
count(­$ar­ray);
is specific value in array?
in_arr­ay(­$ne­edle, $array);

Output

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
echo("l­ine­1\n­lin­e2­\n");
print variable type and data
print_­r($­var);
print variable type and data (detailed)
var_du­mp(­$var);
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;
}
foreach - do something for each element in array
foreac­h($­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;
continue - skip current round
continue;

PHP Filesystem Functions

check if file exists
file_e­xis­ts(­"­/ro­ot/­fil­e.t­xt");
open a file for reading only
$file = fopen(­"./../f­ile.tx­t", "­r");
get size of file in bytes
filesi­ze(­"­fil­e.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
fwrite­($file, "some text\n­");
close file after all operations
fclose­($f­ile);
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
Server and execution enviro­­nment inform­­ation
$_SE­R­VE­­R['­­SER­­VE­R­_­AD­­DR']
Server IP
$_SE­R­VE­­R['­­REQ­­UE­S­T­_M­­ETH­­OD­']
Which request method was used to access the page; i.e. '­GET­', '­HEA­D', '­POS­T', '­PUT­'
$_SE­R­VE­­R['­­HTT­­P_­U­S­ER­­_AG­­EN­T']
User-A­­gent: header from the current request
$_SE­R­VE­­R['­­REM­­OT­E­_­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

Badges

Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 3

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.