Using Conditional Statements with the Xdebug Extension

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.

Introduction

Debugging a PHP application can be a challenging task, particularly when it’s necessary to evaluate the more complex facets of a program. By these I mean which functions are called by it, in what order, or even the performance of its different modules.

In this situation, it’s recommended that you use a third-party debugging library. You’ll find a variety of these on the web these days. If you’re planning to pick up a package that lets you debug your PHP applications in a truly painless way, then you might want to take a closer look at the Xdebug extension. It’s a friendly and powerful piece of software that will permit you to perform all sorts of debugging tasks, with minimal configuration requirements and an easy learning curve.

And speaking of debugging tasks, you’ll probably recall that I left off the last article discussing the use of the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions, which came in handy for keeping track of which lines are executed by a targeted PHP application.

Speaking more specifically, in that tutorial I demonstrated how to utilize these functions to determine the execution flow of a simple script that created an instance of a sample class, and then called some of its methods. However, it’s much more interesting to use these functions to check the flow of a program when working with conditional statements.

Undoubtedly, determining what code block is executed by an application, based on certain conditions, is a task that programmers have to face on a frequent basis, and the less effort this process involves, the better. Thus, provided that you’re interested in learning how to employ the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions to debug conditional statements within a PHP program, in this fifth part of the series I’ll be coding for you some illustrative examples that will show you how to perform this debugging procedure in an approachable fashion.

Now, let’s get rid of the preliminaries and start learning how to use the X-debug extension to work with conditionals in PHP. Let’s jump in!

{mospagebreak title=Review: the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions}

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.

{mospagebreak title=Extending the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions}

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.

{mospagebreak title=Debugging conditionals with the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions}

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:

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 the ‘rand()’ function returns a value > 5


Email :john@domain.com


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

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!

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye