Profiling PHP with Xdebug
Enable Xdebug profiling and analyze callgrind output to find slow functions.
Profiling PHP with Xdebug is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is Xdebug?
Xdebug is a PHP extension that provides debugging, code coverage, and profiling capabilities. The profiler records how long every function call takes and how many times it is called.
Installing Xdebug
Install via PECL or package manager.
# Via PECL:
$ pecl install xdebug
# Via apt:
$ apt install php-xdebug
# Verify:
$ php -v | grep XdebugEnabling the Profiler
Configure Xdebug in php.ini to enable profiling.
# php.ini
[xdebug]
zend_extension=xdebug.so
xdebug.mode=profile
xdebug.output_dir=/tmp/xdebug
xdebug.profiler_output_name=cachegrind.out.%tTrigger-Based Profiling
Instead of profiling every request, use a trigger to profile on demand.
# php.ini
xdebug.mode=profile
xdebug.start_with_request=trigger
# Trigger via query parameter or cookie:
curl "https://example.com/?XDEBUG_PROFILE=1"Callgrind Output
Xdebug writes a cachegrind.out.* file. This format is read by profiling visualisers.
Analysing with KCachegrind / QCachegrind
Open the callgrind file in KCachegrind (Linux) or QCachegrind (macOS/Windows) to see: function call count, total time, self time, and the call tree.
Analysing with WebGrind
WebGrind is a browser-based callgrind viewer — easier to set up than a desktop tool.
$ docker run -p 8080:80 jokkedk/webgrind -v /tmp/xdebug:/tmpReading Profiler Output
Look for functions with the highest "Inclusive Cost" (total time including callees) vs "Self Cost" (time in the function body only). Self cost reveals the actual bottleneck.
Common Bottlenecks Found
Typical findings: N+1 database queries, inefficient regex, redundant API calls, unoptimised Eloquent relationships, and expensive view rendering.
Profiling vs Debugging
Profiling measures performance (time, call counts). Debugging (also an Xdebug feature) pauses execution at breakpoints for code inspection. Use separate Xdebug modes.
Blackfire as an Alternative
Blackfire.io is a commercial profiling tool with a polished UI, CI integration, and performance assertions. Preferred for team environments over raw Xdebug profiling.
Summary
Enable Xdebug profiler mode in php.ini, trigger a request, and open the callgrind file in KCachegrind or WebGrind. Focus on functions with the highest inclusive and self cost to find bottlenecks.
Quick Check
What does "self cost" mean in a profiler report?
Frequently asked questions
Is the “Profiling PHP with Xdebug” lesson free?
Yes — the full text of “Profiling PHP with Xdebug” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Profiling PHP with Xdebug”?
Enable Xdebug profiling and analyze callgrind output to find slow functions. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Profiling PHP with Xdebug” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Profiling PHP with Xdebug
- OPcache: Bytecode Caching
- Optimizing Database Queries
- Memory Management and Performance Tips