Mohamed Sharaf

Underused PHP functions worth knowing

A short tour of handy PHP features from a classic Nettuts+ list, updated for modern PHP.

· 1 min read

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(); now function sum(...$numbers).
  • Finding files by pattern: glob('/var/log/*.log'), simpler than looping over scandir().
  • Memory and CPU usage: memory_get_usage(), memory_get_peak_usage() and getrusage() for quick profiling.
  • Magic constants: __FILE__, __DIR__, __LINE__, __FUNCTION__ for logs and paths.
  • Unique IDs: the article used uniqid(); for anything security-related use bin2hex(random_bytes(16)) instead, because uniqid() is predictable.
  • Storing data: json_encode() rather than serialize(), and never unserialize() data from users. It can be turned into an attack.
  • Compressing strings: gzcompress() and gzuncompress().
  • 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+).

ShareWhatsAppLinkedInXTelegramFacebook