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

Extending 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

To be frank, the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions really start to shine when used to keep track of which lines are executed by a PHP application based on a certain condition.

To exemplify this specific situation, I’m going to use the already familiar “User” class that you learned in the prior section, along with a simple script which will change its execution flow according to a semi-random value generated by the built-in PHP “rand()” function.

Of course, in this case, the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions will be employed to keep track of which code block is executed by the script in question.

The code sample that shows how to use these functions with a basic conditional statement is as follows:

class User{

private $firstName;

private $lastName;

private $email;

public function __construct($firstName,$lastName,$email){

if(!$firstName){

throw new Exception('Invalid parameter First Name.');

}

if(!$lastName){

throw new Exception('Invalid parameter Last Name.');

}

if(!$email){

throw new Exception('Invalid parameter Email.');

}

$this->firstName=$firstName;

$this->lastName=$lastName;

$this->email=$email;

}

// get first name

public function getFirstName(){

return $this->firstName;

}

// get last name

public function getLastName(){

return $this->lastName;

}

// get email

public function getEmail(){

return $this->email;

}

}


xdebug_start_code_coverage();

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

if(rand(1,10)<5){

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

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

}

else{

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

}

var_dump(xdebug_get_code_coverage());



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


First Name :John

Last Name :Doe


array

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

array

8 => int 1

11 => int 1

14 => int 1

17 => int 1

18 => int 1

19 => int 1

20 => int 1

23 => int 1

36 => int 1

37 => int 1

38 => int 1

39 => int 1

40 => int 1

44 => int 1

*/


Definitely, the above example shows in a nutshell how useful the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions can be when it comes to debugging conditional statements within a PHP script.

In this case, the “rand()” PHP native function is utilized in conjunction with an “if” conditional statement to create a simple situation where there are two blocks of code that can be invoked. Logically, this example shows the sequence of lines executed when the “rand()” function returns a value less than 5, in this way demonstrating the functionality of the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions.

So far, so good, right? You hopefully understand how simple it is to debug conditional statements with the two functions shown before. The previous script would be rather incomplete, however, if I don’t show you the output it produces when the value returned by the “rand()” function is greater than 5.

Therefore, in the last section of this article I will represent this condition through a functional script, in this manner finishing this quick overview on using the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions included with the X-debug extension.

Click on the link below and read the next few lines, please.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: