Type managment functions
|
is_double(), is_float(), is_real()
|
same function |
is_long(), is_int(), is_integer()
|
same function |
|
|
|
if $var is a whole number, a string or a double |
|
is $var. is a number or a numerical chain |
|
if $var. is a valid function name |
Data type conversion
|
|
Ret: 42 |
|
|
Ret: 1.2 |
|
|
Ret: '1.2' |
Varible scope and constant definition
|
Will exists beyond its scope |
|
Ex: echo 'Value'.PI; |
PHP variables need not be declared in advance before being used
File system Modes
|
read-only from start (File pointer starts at the beginning of the file) |
|
read-write from start |
|
write-only (Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file) |
|
read-write (erases content from file and creates it if it desn't exist) |
|
append-only to EOF (existing data is preserved, creates new file if doesn't exist) |
|
read-write - append to EOF |
|
create new file for write only (ret. FALSE an ERROR if file already exists) |
|
creates a new file for read/write (ret. FALSE an ERROR if file already exists) |
fseek() can only work in mode w+ and NOT with a or a++
Functions indicating the state of the variables
|
if the var. exists |
|
removes the var. passed in parameter |
|
return true if var. is null |
Get variables from $_FILES
['OBJ FILE NAME']['name']
-> gets name of file selected in a file object |
['OBJ FILE NAME']['tmp_name']
-> gets temp name saved in server, waiting for destination transfer |
['OBJ FILE NAME']['size']
-> gets the file's size in octets |
['OBJ FILE NAME']['type']
-> gets the MIME type of the file |
// File example foreach($_FILES as $curImg => $photo){ $file = $_FILES[$curImg]["name"]; //img.jpeg $tmpName = $_FILES[$curImg]["tmp_name"]; //tmp/phps6L $size = filesize($tmpName)/1024; //43 $ext = pathinfo($file, PATHINFO_EXTENSION); //jpeg $type = filetype($tmpName); //file $mimeType = $_FILES[$curImg]["type"]; //image/jpeg } move_uploaded_file($tmpName, "$path$file"))
String functions
|
Ret. array without 'sep' |
|
|
Ret. length of string |
|
|
|
|
|
strip_tags(str,allowed_tags)
|
|
|
|
//Exemple of substr de $adresse = 127.0.0.1 $class = substr($adresse, 0, 3) // => 127 $class = substr($adresse, 3) // => .0.0.1 $class = substr($adresse, -3); // => 0.1 $class = substr($adresse, 0, -3); // => 127.0.
Code reusability
|
Stops script if error |
require_once('script.ph')
|
|
Warning if error |
include_once('script.php')
|
_once -> makes sure that the code will be included or re-evaluated only once
Image generation
header('Content-type: image/jpeg')
|
$img = ImageCreate(width, height)
|
$img = ImageCreateTrueColor(width, height)
|
$img = ImageCreateFromPNG|JPEG|GIF;
//create from existing image |
ImageColorAllocate($img, r,v,b) // rvb in 0 to 255
|
Imagegammacorrect($img, int gamma_in,int gamma_out)
|
ImageFill($img, x start, y start, color)
|
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $color);
|
imagestring($img, $font, $x, $y, $string, $color);
|
<?php header("Content-Type: image/png"); $im = imagecreate(110, 20); $background_color = imagecolorallocate($im, 0, 0, 0); $text_color = imagecolorallocate($im, 233, 14, 91); imagestring($im, 1, 5, 5, "A Simple Text String", $text_color); imagepng($im); imagedestroy($im); ?>
|
|
Arrays
|
No need to specifiy index |
|
Nb. elements in array |
|
Nb. elements in array |
array_count_value(array)
|
value=>freq |
array_diff(arr1,arr2)
|
array_filter(arr,function)
|
|
array_intersect(arr1,arr2)
|
array_merge(arr1,arr2)
|
|
array_push(arr,var1, var2...)
|
|
array_search(needle, arr)
|
array_walk(arr,function)
|
in_array(needle,haystack)
|
|
smaller < bigger |
|
bigger > smaller |
foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement
|
`array_slice($array, 0, 3); |
returns first 3 elements |
//Use of array_slice()
$input = array("a", "b", "c", "d", "e"); $output = array_slice($input, 2); // returns "c", "d", and "e" $output = array_slice($input, -2, 1); // returns "d" $output = array_slice($input, 0, 3); //returns "a", "b", and "c"
PHP integrated arrays
|
|
|
|
|
|
|
extract(arr,type, pref)
|
get vals. from assoc. arr; |
|
ow existing vars. |
|
won't ow existing vars |
|
|
To get values easily from theses arrays, use function extract(). If we have:
> $_POST['name']
> $_POST['surname']
Using the function: extract($_POST, EXTR_OVERWRITE) will give us:
>$name
>$surname
-------------------------------------
>EXTR_PREFIX_ALL // create new vars. with the prefix spcified in 3rd param for all keys present in the array
>EXTR_PRERFIX_ALL // creates new vars. with prefix specified in 3rd param only for invalid exsting variable names such as $1
Environnement variables from $_SERVER
|
path to curr. script |
|
Ex: Localhost |
|
root of curr.script |
|
IP requesting curr. page |
|
client port -> server |
|
abs. path to curr.script |
|
Server port used |
|
URI is for page access |
>PHP_SELF //return value is relative to root document
>SERVER_PORT //server port used for communication (usually :80 but if using SSL can be replaced par number of secured HTTP
>REQUEST_URI // can be for example '/index.html'
|
|
File System Functions
fopen('filename', 'mode')
|
|
|
without 2nd param, fgets read line till EOL |
|
|
can also be url |
|
|
filesize('folder/file')
|
|
doesn't work with HTTP or FTP |
|
|
|
fgetcsv(handle,len,sep)
|
|
|
goto line of curr. offset (needs r+ or rw+ mode) |
|
goto start of file |
chmod(file, unix rights)
|
|
deletes file |
|
is_writeable('filename')
|
is_readable('filename')
|
pathinfo($file, PATHINFO_EXTENSION);
|
Ret. ex: pdf |
|
Ret: file or dir |
|
Ret. file size ( / 1024 to convert to octets) |
//Example of openinng a file: $handle = fopen("file.txt", "w"); if (!$handle) { echo 'Can't create file'; exit; } else { while(!feof($handle)) { $line = fgets($handle, 255); fputs($handle, $line.'suffix'); $appendLine = fgets($handle, 255); echo $appendLine.'<br>'; } fclose($handle);
Folder system functions
|
|
|
|
Returns array of folder content |
//Example with opendir $dir = "/etc/php5/"; if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { echo "filename: $file : filetype: " . filetype($dir . $file) . "\n"; } closedir($dh) } } Example with scandir() $nomDossier = 'dossier'; $tableContDossier = scandir($nomDossier); foreach($tableContDossier as $fic) { echo $fic.'<br>'; }
File transfert functions
is_uploaded_file($_FILES['fichier']['tmp_name']
|
verify file presence in tmp folder |
move_uploaded_file('filename','path/filename')
|
Checks is file comes from tmp folder unlike fonction copy() |
copy('filename','path/filename')
|
Database connection
mysqli_connect('host', 'user', 'pass') or die ("Can't establish connecion to databse")
|
mysqli_select('databsename');
|
$queryResult = mysqli_query("SELECT * FROM table");
|
mysqli_error() // boolean to return error if table doesn't exist
|
$row = mysql_fetch_array($result, MYSQL_ASSOC)
|
|
|
mysqli_num_rows() //ret. nb. rows from query
|
Example fetching array values:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) {
$name = $row['name'];
$country = $row['country'];
}
PHP regular expressions
Check if $string
matches $expression
|
preg_match('(docx|doc|xls|pptx)', $extension);
|
PHP regular expressions
Check if $extension
is either "docx", "doc", "xls" or "pptx" |
preg_match('(docx|doc|xls|pptx)', $extension);
|
|