PHP Quick Reference Summary
Code Structure
- PHP code is enclosed within
<?php ... ?>
.
- Variables are prefixed with
$
.
- Functions are defined using the
function
keyword.
- Comments can be single-line (
//
) or multi-line (/* ... */
).
Reusing Code
- Use
include
, require
, include_once
, and require_once
to reuse code from other files.
include
generates a warning on failure, while require
generates a fatal error.
_once
versions prevent redefinition of functions.
Nomenclature Rules
- Variable names start with
$
and can include letters, numbers, and underscores, but cannot start with a number.
- Variable names are case-sensitive.
Visibility & Scope
- PHP variables have a single scope, except inside functions.
- Use
global
keyword or $GLOBALS
array to access global variables within functions.
Data Types
- Scalar data types:
boolean
, integer
, float
, string
.
NULL
represents a variable with no value.
- Implicit type conversion is common; explicit typecasting uses
(type)$variable
.
Operators
- Arithmetic operators:
+
, -
, *
, /
, %
.
- String concatenation:
.
.
- Comparison operators:
==
(loose), ===
(strict), !=
, <>
, !==
.
- Logical operators:
&&
, ||
, !
.
- Bitwise operators:
&
, |
, ^
, ~
, <<
, >>
.
Constants
- Define constants using
define($name, $value, [$case_insensitive])
.
- Constants do not start with
$
and cannot be changed once set.
- Magic constants:
__LINE__
, __FILE__
, __DIR__
, __CLASS__
, __METHOD__
.
Variable Management
- Functions for variable management include
empty()
, floatval()
, gettype()
, intval()
, is_#()
, serialize()
, settype()
, strval()
, unserialize()
, unset()
.
Arrays
- Arrays can be defined using
array()
or []
.
- Associative arrays use key-value pairs.
- Array manipulation functions include
array_chunk()
, array_fill()
, array_flip()
, array_key_exists()
, array_reverse()
, array_values()
, count()
, ksort()
.
Date & Time
- Functions include
getDate()
, checkdate()
, date()
.
- Common format characters for
date()
: d
, j
, D
, l
, N
, S
, w
, z
, W
, F
, m
, M
, n
, t
, L
, Y
, y
, a
, A
, g
, G
, h
, H
, i
, s
, U
, e
, P
.
File System
- File handling functions:
basename()
, file_exists()
, filesize()
, fileatime()
, chmod()
, pathinfo()
, dirname()
, glob()
, is_#()
, chdir()
, closedir()
, getcwd()
, mkdir()
, opendir()
, readdir()
, rewinddir()
, rmdir()
, scandir()
, disk_free_space()
, rename()
, fclose()
, fopen()
, file_get_contents()
, fread()
, ftruncate()
, fwrite()
, file_put_contents()
, fseek()
, ftell()
, rewind()
, fflush()
.
Math Functions
- Common math functions:
abs()
, base_convert()
, ceil()
, dechex()
, deg2rad()
, exp()
, floor()
, fmod()
, hexdec()
, log10()
, log()
, pi()
, pow()
, rad2deg()
, rand()
, round()
, sqrt()
.
Output & Formatting
- Output functions:
echo
, print
, print_r()
, printf()
, sprintf()
.
- Format specifiers for
printf()
include %b
, %c
, %d
, %e
, %f
, %s
, %x
, %X
, %%
.
Strings
- String manipulation functions:
strlen()
, strpos()
, strrpos()
, stripos()
, strripos()
, strtolower()
, strtoupper()
, chr()
, ord()
, explode()
, implode()
, ltrim()
, rtrim()
, trim()
, strip_tags()
, substr()
, substr_count()
, str_replace()
, ucwords()
.
Conditional Execution
- Use
if
, elseif
, else
for conditional statements.
- Use
switch
for multiple conditions.
- Ternary operator:
(condition) ? trueCode : falseCode
.
Exception Handling
- Use
try
, catch
, and throw
for exception handling.
- Set a default exception handler with
set_exception_handler()
.
Looping
- Loop structures:
while
, do...while
, for
, foreach
.
- Use
break
to exit a loop and continue
to skip to the next iteration.
User Functions
- Define functions using
function
.
- Default argument values and passing by reference are supported.
Superglobals
- Predefined arrays:
$GLOBALS
, $_SERVER
, $_GET
, $_POST
, $_FILES
, $_SESSION
, $_ENV
, $_COOKIE
.
Miscellanea
- Use
@
to suppress errors.
- Use backticks
`command`
to execute shell commands.
- Use
eval()
to evaluate PHP code within a string.