Underused PHP functions worth knowing
A short tour of handy PHP features from a classic Nettuts+ list, updated for modern PHP.
From the archive: in 2011 I shared “9 Useful PHP Functions and Features You Need to Know” by Nettuts+ (Envato Tuts+) on my old blog. This page is a short summary in my own words; the original belongs to its author.
A popular 2010 Nettuts+ article listed PHP features many developers never noticed. The ones still worth knowing, with today’s versions:
- Any number of arguments: then
func_get_args(); nowfunction sum(...$numbers). - Finding files by pattern:
glob('/var/log/*.log'), simpler than looping overscandir(). - Memory and CPU usage:
memory_get_usage(),memory_get_peak_usage()andgetrusage()for quick profiling. - Magic constants:
__FILE__,__DIR__,__LINE__,__FUNCTION__for logs and paths. - Unique IDs: the article used
uniqid(); for anything security-related usebin2hex(random_bytes(16))instead, becauseuniqid()is predictable. - Storing data:
json_encode()rather thanserialize(), and neverunserialize()data from users. It can be turned into an attack. - Compressing strings:
gzcompress()andgzuncompress(). - Code that runs at the end:
register_shutdown_function(), handy for logging fatal errors.
Read the original: 9 Useful PHP Functions and Features You Need to Know, by Nettuts+ (Envato Tuts+).