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

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

Before I start explaining how to use the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions to debug conditional statements within a PHP application, I’m going to reintroduce the last example that I created in the preceding tutorial. It was aimed at illustrating how to utilize the same functions for keeping track of which lines were executed by a simple function and a sample class.

The code samples corresponding to these practical examples originally looked like this:

(example on debugging program flow with a PHP function)


xdebug_start_code_coverage();

function displayIntegers(){

for($i=1;$i<=10;$i++){

echo $i;

}

}

displayIntegers();

var_dump(xdebug_get_code_coverage());

/* displays the following

12345678910


array

'pathtoexampleflow_debugging_example.php' =>

array

4 => int 1

5 => int 1

6 => int 1

7 => int 1

8 => int 1

9 => int 1

10 => int 1

*/



(example on debugging program flow with a PHP class)


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;

}

}


try{

xdebug_start_code_coverage();

$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 />';

var_dump(xdebug_get_code_coverage());

 

/* displays the following

First Name :John

Last Name :Doe

Email :john@domain.com

array

'pathtoexampleflow_debugging_example.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

27 => int 1

31 => int 1

36 => int 1

37 => int 1

38 => int 1

39 => int 1

41 => int 1

*/

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


As illustrated above, the combination of the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions is very convenient for determining the program flow of a PHP script. In the first example, these functions are used in conjunction to determine the sequence of lines that are executed by a simple PHP function, while in the second case, this same process is applied to a primitive class.

At this point, I’m pretty sure that you have already grasped the logic that drives the two previous code samples, since they’re very easy to follow. With that idea in mind, it’s time to continue exploring the  functionality provided by the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions.

Thus, as I expressed in the introduction, it would be quite useful to demonstrate how these functions can be used with conditional statements. In the upcoming section I’ll be coding a brand new example, which hopefully will demonstrate how to utilize these functions with a simple conditional instruction.

As usual, to see how this example will be developed, please click on the link shown below and keep reading.



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