PHP
  Home arrow PHP arrow Page 3 - Using Conditional Statements with the Xdebug Extension
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Using Conditional Statements with the Xdebug Extension
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2009-03-02


    Table of Contents:
  • Using Conditional Statements with the Xdebug Extension
  • Review: the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions
  • Extending the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions
  • Debugging conditionals with the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using Conditional Statements with the Xdebug Extension - Extending the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions
    ( Page 3 of 4 )

    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
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek