PHP Functions Cheatsheet
Quick reference for PHP functions: string, array, date & time, math, file operations, sessions, cookies, and utility functions.
| Function | Description | Example | Category |
|---|---|---|---|
| strlen() | Get string length | strlen("Hello"); | String |
| strtoupper() | Convert to uppercase | strtoupper("hello"); | String |
| strtolower() | Convert to lowercase | strtolower("HELLO"); | String |
| substr() | Get substring | substr("Hello", 1, 3); | String |
| strpos() | Find position of substring | strpos("Hello","e"); | String |
| count() | Count array elements | count([1,2,3]); | Array |
| array_push() | Add element to end | array_push($arr, 4); | Array |
| array_pop() | Remove last element | array_pop($arr); | Array |
| array_merge() | Merge arrays | array_merge($arr1, $arr2); | Array |
| in_array() | Check if exists | in_array(3, [1,2,3]); | Array |
| date() | Format date/time | date("Y-m-d H:i:s"); | Date & Time |
| time() | Current timestamp | time(); | Date & Time |
| strtotime() | Parse string to timestamp | strtotime("next Monday"); | Date & Time |
| abs() | Absolute value | abs(-5); | Math |
| round() | Round float | round(3.14); | Math |
| ceil() | Round up | ceil(3.14); | Math |
| floor() | Round down | floor(3.14); | Math |
| rand() | Random number | rand(1,100); | Math |
| fopen() | Open file | fopen("file.txt","r"); | File |
| fread() | Read file | fread($file, filesize("file.txt")); | File |
| fwrite() | Write file | fwrite($file, "Hello"); | File |
| fclose() | Close file | fclose($file); | File |
| file_exists() | Check if file exists | file_exists("file.txt"); | File |
| session_start() | Start session | session_start(); | Session & Cookie |
| $_SESSION | Access session variable | $_SESSION["user"]="Alice"; | Session & Cookie |
| setcookie() | Set cookie | setcookie("user","Alice",time()+3600); | Session & Cookie |
| $_COOKIE | Access cookie | $_COOKIE["user"]; | Session & Cookie |
| print_r() | Print human-readable | print_r($arr); | Utility |
| var_dump() | Detailed variable info | var_dump($var); | Utility |
| die() / exit() | Terminate script | die("Error"); | Utility |