Cheatography
https://cheatography.com
Language basics
script start |
<?php |
script end |
?> or nothing |
command termination |
; |
inline comment |
|
block comment |
|
run script from file (if error throws warning) |
include('script.php');
|
run script from file (errors are fatal) |
require('script.php');
|
Variables
declaration without value |
|
string declaration |
|
integer declaration |
|
double declaration |
|
empty array declaration |
|
array with elements declaration |
$a = array("text",1,2.3);
|
boolean declaration |
|
null declaration |
|
Variable operations
addition |
|
divison |
|
multiplication |
|
exponentiation |
|
substraction |
|
string + int (string get converted) #1 |
|
string + int (string get converted) #1 |
"shitsof13" + 13
-> 13 |
string + string |
"2dogs" + "5dogs"
-> 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 = " stranger!";
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 comperation |
|
is var equal to int? |
|
is var lower than int? |
|
is var bigger than int? |
|
is var lower than 5 and not equal to zero |
|
is string equal to string? |
($str == "dangerous")
|
is num 1 or 4? |
|
Array operations
get array element by index |
|
push data to end of array |
array_push($array, $data);
OR $array[] = $data;
|
push data to specific index of array |
$array['myColor'] = "#FFF";
|
remove & get last element of array |
|
get count of elements in array |
|
is specific value in array? |
in_array($needle, $array);
|
|
|
Output
simple print |
echo($variable . " and some text");
|
simple print #2 |
|
simple print #3 (must be without parentheses) |
|
print multiple lines |
echo("line1\nline2\n");
|
print variable type and data |
|
print variable type and data (detailed) |
|
conditional printing |
echo($var1 == 1 ? "its 1!" : "its not 1!");
|
Loops
for - classic loop (start instruction; condition on start of each round;end instruction of each round ) |
for ($i = 1; $i <= 10; $i++) { echo $i; }
|
foreach - do something for each element in array |
foreach($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 |
|
continue - skip current round |
|
PHP Filesystem Functions
check if file exists |
file_exists("/root/file.txt");
|
open a file for reading only |
$file = fopen("./../file.txt", "r");
|
get size of file in bytes |
filesize("file.txt");
|
print all bytes from fopened file |
echo fread($file, filesize("file.txt"));
|
open a file for reading only |
$file = fopen("./../file.txt", "w");
|
write to fopened file |
fwrite($file, "some text\n");
|
close file after all operations |
|
oneline file write |
file_put_contents($filename, $data);
|
oneline file read |
$fileContent = file_get_contents($fileName);
|
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 environment information |
$_SERVER['SERVER_ADDR'] |
Server IP |
$_SERVER['REQUEST_METHOD'] |
Which request method was used to access the page; i.e. 'GET', 'HEAD', 'POST', 'PUT' |
$_SERVER['HTTP_USER_AGENT'] |
User-Agent: header from the current request |
$_SERVER['REMOTE_ADDR'] |
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 |
Associative array of variables passed to the current script via the URL parameters |
$_POST |
An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request |
$_FILES |
An associative array of items uploaded to the current script via the HTTP POST method |
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by davidsykora