0% found this document useful (0 votes)
62 views

PHP Cheat Sheet 1

This document provides a cheat sheet for PHP essentials, summarizing key elements of the PHP language including basics, flow control, variables, arrays, output, loops, and useful built-in variables. It lists PHP commands and functions for scripts, comments, inclusion, variables, data types, operations, conditionals, arrays, printing, and loops in 1-2 concise sentences along with examples for each. Built-in variables are also summarized that provide server information, request data, and current script details.

Uploaded by

kaxam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

PHP Cheat Sheet 1

This document provides a cheat sheet for PHP essentials, summarizing key elements of the PHP language including basics, flow control, variables, arrays, output, loops, and useful built-in variables. It lists PHP commands and functions for scripts, comments, inclusion, variables, data types, operations, conditionals, arrays, printing, and loops in 1-2 concise sentences along with examples for each. Built-in variables are also summarized that provide server information, request data, and current script details.

Uploaded by

kaxam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

PHP Essentials Cheat Sheet

by davidsykora via cheatography.com/43180/cs/12892/

Language basics Flow control

script start <?php simple condition if (true){

script end ?> or nothing echo("condition pass");


}
command termin​ation ;

inline comment // simple oneliner is var1 equal to var2? if ($var1 == $var2){


echo("it is.");
block comment /* multiline text */
} else {
run script from file (if error throws inclu​de(​'sc​rip​t.p​hp');
echo("it ins't.")
warning)
}
run script from file (errors are fatal) requi​re(​'sc​rip​t.p​hp');
two vars comper​ation ($var1 == $var2)

Variables is var equal to int? ($var1 == 4)

declar​ation without value $vari​abl​eName; is var lower than int? ($int < 4)

string declar​ation $string = "some text"; is var bigger than int? ($int > 4)

integer declar​ation $int = 123; is var lower than 5 and not equal to ($int < 5 && $int != 0)
zero
double declar​ation $doble = 1.2
is string equal to string? ($str == "​dan​ger​ous​")
empty array declar​ation $array = array();
is num 1 or 4? ($num == 1 || $num == 4)
array with elements declar​ation $a = array(​"​tex​t",1​,2.3);

boolean declar​ation $bool = true; Array operations

null declar​ation $empty = null; get array element by $arra​y[i​ndex]


index
Variable operations push data to end of array array​_pu​sh(​$array, $data); OR

addition $int1 + $int2 -> 12 $array[] = $data;

divison 6 / 3 -> 2 push data to spec​ific $arra​y['​myC​olor'] = "​#FF​F";


index of array
multip​lic​ation 6 * 6 -> 36
remove & get last array​_po​p($​arr​ay);
expone​nti​ation 2 ** 4 -> 16
element of array
substr​action 2 - 1 -> 1 get count of elements in count​($a​rray);

string + int (string get "13​shi​ts" + 13 -> 26 array

converted) #1 is specific value in array? in_ar​ray​($n​eedle, $array);

string + int (string get "sh​its​of1​3" + 13 -> 13


converted) #1 Output

string + string "2d​ogs​" + "​5do​gs" -> 7 simple print echo(​$va​riable . " and some
strings concat $str1 . $str2 -> hello stranger! text");

string and int concat "numero uno - " . 1; -> numero simple print #2 echo 1;
uno - 1
simple print #3 (must be without echo 1,2; -> 12
unset - delete variable unset​($v​ari​able); parent​heses)

print multiple lines echo(​"​lin​e1​\nli​ne2​\n");


$int1 = 5;
$int2 = 7; print variable type and data print​_r(​$var);
$str1 = "​hello ";
print variable type and data var_d​ump​($v​ar);
$str2 = " strang​er!​"; (detailed)

By davidsykora Published 6th October, 2017. Sponsored by ApolloPad.com


cheatography.com/davidsykora/ Last updated 21st September, 2017. Everyone has a novel in them. Finish Yours!
Page 1 of 2. https://apollopad.com
PHP Essentials Cheat Sheet
by davidsykora via cheatography.com/43180/cs/12892/

Output (cont) Useful variables

condit​ional echo(​$var1 == 1 ? "its 1!" : "its not $_SERVER Server and execution enviro​​nment inform​​ation
printing 1!"); $_SE​R​VE​​R[ Server IP
'​​SE​R​V​ER​​_
Loops AD​​DR']

$_SE​R​VE​​R[ Which request method was used to access the page; i.e.
for - classic loop (start instru​ction; for ($i = 1; $i <= 10;
'​​RE​Q​U​ES​​T 'GET', 'HEAD', 'POST', 'PUT'
condition on start of each round;end $i++) {
_M​​ET​H​OD'
instru​ction of each round ) echo $i;
]
}
$_SE​R​VE​​R[ User-A​​gent: header from the current request
fore​ach - do something for each forea​ch(​$array as '​​HT​T​P​_U​​S
element in array $arrayElement){ ER​​_A​G​ENT
']
var_dump($arrayElement;)
} $_SE​R​VE​​R[ The IP address from which the user is viewing the current
'​​RE​M​O​TE​​_ page
while - do something if cond is true while(true){
AD​​DR']
again and again if is still true echo("infinity!");
__FILE__ The full path and filename of the current script file
}
__DIR__ The directory of the file
break - escape from loop break;
__LINE__ Current line number in script file
cont​inue - skip current round conti​nue;
$_GET Associ​ative array of variables passed to the current script
via the URL parameters
PHP Filesystem Functions
$_POST An associ​ative array of variables passed to the current
check if file exists file_​exi​sts​("/r​oot​/fi​le.t​xt​"); 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
open a file for reading $file = fopen(​"./../f​ile.tx​t",
as the HTTP Conten​t-Type in the request
only "​r");
$_FILES An associ​ative array of items uploaded to the current script
get size of file in bytes files​ize​("fi​le.t​xt​"); via the HTTP POST method

print all bytes from echo fread(​$file,


fopened file filesi​ze(​"​fil​e.t​xt"));

open a file for reading $file = fopen(​"./../f​ile.tx​t",


only "​w");

write to fopened file fwrit​e($​file, "some text\n​");

close file after all fclos​e($​file);


operations

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 r = Read


argument) r+ = Read,write and prepend
w = Write, truncate
w+ = Read and write, truncate a Write,
append
a+ = Read and write, append

By davidsykora Published 6th October, 2017. Sponsored by ApolloPad.com


cheatography.com/davidsykora/ Last updated 21st September, 2017. Everyone has a novel in them. Finish Yours!
Page 2 of 2. https://apollopad.com

You might also like