PHP Functions Cheatsheet

Quick reference for PHP functions: string, array, date & time, math, file operations, sessions, cookies, and utility functions.

FunctionDescriptionExampleCategory
strlen()Get string lengthstrlen("Hello");String
strtoupper()Convert to uppercasestrtoupper("hello");String
strtolower()Convert to lowercasestrtolower("HELLO");String
substr()Get substringsubstr("Hello", 1, 3);String
strpos()Find position of substringstrpos("Hello","e");String
count()Count array elementscount([1,2,3]);Array
array_push()Add element to endarray_push($arr, 4);Array
array_pop()Remove last elementarray_pop($arr);Array
array_merge()Merge arraysarray_merge($arr1, $arr2);Array
in_array()Check if existsin_array(3, [1,2,3]);Array
date()Format date/timedate("Y-m-d H:i:s");Date & Time
time()Current timestamptime();Date & Time
strtotime()Parse string to timestampstrtotime("next Monday");Date & Time
abs()Absolute valueabs(-5);Math
round()Round floatround(3.14);Math
ceil()Round upceil(3.14);Math
floor()Round downfloor(3.14);Math
rand()Random numberrand(1,100);Math
fopen()Open filefopen("file.txt","r");File
fread()Read filefread($file, filesize("file.txt"));File
fwrite()Write filefwrite($file, "Hello");File
fclose()Close filefclose($file);File
file_exists()Check if file existsfile_exists("file.txt");File
session_start()Start sessionsession_start();Session & Cookie
$_SESSIONAccess session variable$_SESSION["user"]="Alice";Session & Cookie
setcookie()Set cookiesetcookie("user","Alice",time()+3600);Session & Cookie
$_COOKIEAccess cookie$_COOKIE["user"];Session & Cookie
print_r()Print human-readableprint_r($arr);Utility
var_dump()Detailed variable infovar_dump($var);Utility
die() / exit()Terminate scriptdie("Error");Utility