Home arrow PHP arrow Page 4 - Using Conditional Statements with the Xdebug Extension

Debugging conditionals with the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions - PHP

In this fifth part of a series on using the Xdebug extension to help debug your PHP programs, we'll take a closer look at the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions. Specifically, we'll see how we can extend their usage when working with conditional statements. As always, we'll complement theory with a number of hands-on examples.

  1. Using Conditional Statements with the Xdebug Extension
  2. Review: the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions
  3. Extending the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions
  4. Debugging conditionals with the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
March 02, 2009

print this article



To complete the practical example developed in the prior section, it’s necessary to show the output produced by the script when the “rand()” PHP function returns a value greater than five to client code.

In that particular case, the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions will display on screen the sequence of lines being executed within the second block of code, instead of the instructions wrapped in the first one.

But undoubtedly, this process will be better understood if you examine the following code sample, which represents this situation in a clear manner:


$user=new User('John','Doe','john@domain.com');


echo 'First Name :'.$user->getFirstName().'<br />';

echo 'Last Name :'.$user->getLastName().'<br />';



echo 'Email :'.$user->getEmail().'<br />';



/* displays the following when the 'rand()' function returns a value > 5

Email :john@domain.com


 '/path/to/example/program_flow_debug.php' =>


8 => int 1

11 => int 1

14 => int 1

17 => int 1

18 => int 1

19 => int 1

20 => int 1

31 => int 1

36 => int 1

37 => int 1

42 => int 1

44 => int 1


Definitely, the example shown above should be extremely simple for you to grasp. All it does is show the output generated by the xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions when the second block of code is executed by the previous script. See how easy it is to use these functions along with conditional statements to keep track of the flow followed by a PHP application? I bet you do!

The best way to grasp how those functions do their work, however, is with lots of practice. Thus, feel free to tweak all of the code samples included in this tutorial, so you can acquire a more solid background in using these handy functions provided by the Xdebug extension.

Final thoughts

In this fifth chapter of the series, I walked you through using the "xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions, which can be really useful for debugging execution flow of a PHP application.

However, this educational journey of exploring the Xdebug library hasn’t finished yet, since it includes a few other functions that deserve a close analysis. Therefore, in the next article I’ll be taking a look at the “xdebug_index_time()” function, which can be used for benchmarking PHP scripts very easily.

This is my little piece of advice: don’t miss the upcoming tutorial!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: