Debugging with inserted code

Suppose you are debugging and you wish to verify whether a variable in the following code is empty and an array before using it:

$var2 = array();
// ... some code here followed by
$var2[] = 'adam';

Several possible problems could arise for between two the two lines of code by the code which would be in between. We know that on the last line, we expect $var2 to be an empty array. One way to verify would be to insert the following code which would only complain when $var2 is not what we expected it to be.

$var2 = array();
// ... some code    
// testing whether variable is empty and whether it is still an array
if (!is_array($var2)) { print 'error: $var2 is not an array'; }
if (!empty($var2)) { print 'error: $var2 is not empty'; }
$var2[] = 'adam';

A more elegant way to do the same would be to write a function for this as follows:

$var2 = array();
// ... some code    
// it is a good idea to test whether is empty and whether it is still an array   
assertTrue(is_array($var2), '$var2 is not an array');
assertTrue(empty($var2), '$var2 is not empty');
$var2[] = 'adam';

function assertTrue($condition, $msg) {
    if (!$condition) {
        print "Assertion failed: {$msg}";
    }
}

Once again, the system complains only when something goes wrong.

Naturally, the next thing which would cross your mind is that there should be a library or framework for this. In fact, there are many frameworks but the two most commonly used ones are PHPUnit and SimpleTest.