🐘 PHP Advanced

What is PHP profiling and how do you use Xdebug?

Answer

Profiling measures where your PHP application spends time and memory. Xdebug is the most popular PHP debugging and profiling extension. For profiling, enable xdebug.mode=profile — it generates cachegrind files that can be analyzed with KCacheGrind or Webgrind to see a call tree with time spent in each function. Blackfire.io is the production-grade profiler — it has low overhead, integrates with CI/CD, tracks performance over time, and provides recommendations. For simple timing: microtime(true). For memory: memory_get_peak_usage(). Common bottlenecks: N+1 queries (use eager loading), missing database indexes, un-cached repeated computations, and large in-memory data processing. Always profile before optimizing — measure, do not guess.