PHP Cheat Sheet 1
PHP Cheat Sheet 1
declaration without value $variableName; is var lower than int? ($int < 4)
string declaration $string = "some text"; is var bigger than int? ($int > 4)
integer declaration $int = 123; is var lower than 5 and not equal to ($int < 5 && $int != 0)
zero
double declaration $doble = 1.2
is string equal to string? ($str == "dangerous")
empty array declaration $array = array();
is num 1 or 4? ($num == 1 || $num == 4)
array with elements declaration $a = array("text",1,2.3);
string + string "2dogs" + "5dogs" -> 7 simple print echo($variable . " 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($variable); parentheses)
conditional echo($var1 == 1 ? "its 1!" : "its not $_SERVER Server and execution environment information
printing 1!"); $_SERVER[ Server IP
'SERVER_
Loops ADDR']
$_SERVER[ Which request method was used to access the page; i.e.
for - classic loop (start instruction; for ($i = 1; $i <= 10;
'REQUEST 'GET', 'HEAD', 'POST', 'PUT'
condition on start of each round;end $i++) {
_METHOD'
instruction of each round ) echo $i;
]
}
$_SERVER[ User-Agent: header from the current request
foreach - do something for each foreach($array as 'HTTP_US
element in array $arrayElement){ ER_AGENT
']
var_dump($arrayElement;)
} $_SERVER[ The IP address from which the user is viewing the current
'REMOTE_ page
while - do something if cond is true while(true){
ADDR']
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
continue - skip current round continue;
$_GET Associative array of variables passed to the current script
via the URL parameters
PHP Filesystem Functions
$_POST An associative array of variables passed to the current
check if file exists file_exists("/root/file.txt"); script via the HTTP POST method when using
application/x-www-form-urlencoded or multipart/form-data
open a file for reading $file = fopen("./../file.txt",
as the HTTP Content-Type in the request
only "r");
$_FILES An associative array of items uploaded to the current script
get size of file in bytes filesize("file.txt"); via the HTTP POST method