I just threw this together to help in debugging some PHP scripts. As you can see, it is sloppy but I am going to improve it some more.
My debug function has 2 variables passed in, a variable name and a variable value.
Is it possible to just pass in the variable and somehow get the name of the variable without manually doing it like I have it set now?
The Function
<?php
function debug($varname, $var)
{
echo '<br>' . $varname;
// $var is a STRING
if (is_string($var)) {
echo ' (string) = ' . $var . '<br>';
// $var is an ARRAY
} elseif (is_array($var)) {
echo ' (array) = <pre>';
print_r($var);
echo '</pre><br>';
// $var is an INT
} elseif (is_int($var)) {
echo ' (int) = ' . $var . '<br>';
// $var is an OBJECT
} elseif (is_object($var)) {
echo ' (object) = <pre>';
var_dump($var);
echo '</pre><br>';
}
}
The Test
$testString = 'just a test!';
$testArray = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
);
$testInt = 1234567890;
$testObject = new stdClass;
$testObject->someVar1 = 'testing123';
$testObject->someVar2 = '321gnitset';
debug('$testString', $testString);
debug('$testArray', $testArray);
debug('$testInt', $testInt);
debug('$testObject', $testObject);
?>
The Result...
$testString (string) = just a test!
$testArray (array) =
Array
(
[key1] => value1
[key2] => value2
[key3] => value3
)
$testInt (int) = 1234567890
$testObject (object) =
object(stdClass)#1 (2) {
["someVar1"]=>
string(10) "testing123"
["someVar2"]=>
string(10) "321gnitset"
}