Manuale PHP
Input/output:
echo x, y, …;
echo(x,y,…);
print x, y, …;
Conditions:
if(condition){
blocK1
}else{
block2
}
if(condition){
blocK1
}elsif{
block2
}… else {
blockN
}
switch(value) {
case value1: block1;
case value2: block2;
…
default:blockN
}
Loops:
while (condition) {
block;
}
do {
block;
} while (condition);
for(initialization; condition; increment) {
block;
}
foreach(array as $index => $value) {
block;
}
break;
String functions:
int strlen(string); // number of characters
int str_word_count(string); // number of words
void strrev(string); // reverse string
int strpos(substring); // position of substring
void str_replace(oldsubstring, newsubstring, string);
Number functions:
bool is_int(number);
bool is_float(number);
bool is_finite(number);
bool is_infinite(number);
bool is_nan(number);
bool is_numeric(variable);
Math functions:
double pi(); // value of pi
float min(number1, number2, …) // minimum number
float max(number1, number2, …) // maximum number
double abs(number); // absolute value
double sqrt(number); // square root
double round(number); // nearest integer
double floor(number); // nearest lower integer
double ceil(number); // nearest upper integer
bool rand(min,max); // random between min and max
Arithmetic operators:
+ addition
- subtraction
* multiplication
/ division
% modulus
^ exponentiation
Assignment operators:
= assignment
+= add
-= subtract
*= multiply
/= divide
%= mudulize
Comparison operators:
== equal
=== identical (value and type)
!= not equal
!== not identical
> greater than
>= greater than or equal
< less than
<= less than or equal
Functions:
function functionName(parameters) { // definition
block;
}
functionName(parameters); // call
Arrays:
indexed
$variable = array(first, second, …);
$variable = array();
associative
$variable = array(firstkey => firstvalue, secondkey => secondvale, …);
$variable = array();
multidimensional
$variable = array(
array(first, second, …);
array(first, second, …);
array(first, second, …);
…
);
Array functions:
int count(array); // number of elements
sort(array); // ascending order
rsort(array); // descending order
asort(array); // ascending values order
ksort(array); // ascending keys order
arsort(array); // descending values order
krsort(array); // descending keys order
array_fill(value1, value2, …); // fill array positions
array_diff(array1, array2); // difference array
array_push(value); // add at the end
array_pop(array); // remove from the end
array_sum(array); // sum of elements
Superglobals:
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
OOP:
class classname { // class definition
…
}
object = new class; // object creation
private, protected, public // visibility rules
__construct(parameters); // constructor
__destruct(); // desctructor
class subclass extends superclass { // subclass definition
…
}
abstract class classname { // abstract class definition
…
}
interface interfacename { // interface definition
public function1 declaration;
public function2 declaration;
…
}
class classname implements interfacename { // interface implementation
public function1 definition;
public function2 definition;
}
trait traitname { // trait definition
block;
}
class classname { // trait use
use traitname
…
}
Database MySQLi (procedural):
variable = mysqli_connect(server, user, password) // open connection
mysqli_close(variable); // close connection
variable mysqli_query(connection, querystring); // query
int mysqli_num_rows(variable); // number of resulting rows
while($r = mysqli_fetch_assoc(variable)) { // loop through resulting rows
block;
}