note 41508 deleted from function.gettype by nlopess
| From: | nlopess@php.net | Date: | Tue, 13 Apr 2004 10:46:44 +0000 |
| Subject: | note 41508 deleted from function.gettype by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-68060@lists.php.net to get a copy of this message | ||
Note Submitter: MC_Breit @phpws.org
----
Here is an alternative varInformation function,
I use it to debug my scripts.
<?php
/******
* varInfo debug function by MC_Breit
* from phpws.org
* ! Use varInfo('$var') instead of varInfo($var) !
*/
//Define some strings to test the script
$teststring = 'test';
$testinteger = 123;
$testarray = array('Test1', 'Test2');
//A String
varInfo('$teststring');
echo'<br>';
//A Integer
varInfo('$testinteger');
echo'<br>';
//A Array
varInfo('$testarray');
echo'<br>';
//A Empty var..
varInfo('$empty');
echo'<br>';
//Producing an error ;)
if( !varInfo('A name for a variable, which is not valid.. $)(') )
echo varInfo('ERROR');
function varInfo($vtitle)
{
static $error;
if( $vtitle == 'ERROR' )
{
return $error;
}
//Check validation of given variable name
if( !preg_match('/^\$[a-zA-Z0-9]+$/', $vtitle) ) {
$error = '<b>Syntax Error:</b> "'.$vtitle.'" is not a valid
name for a var!';
return FALSE;
}
//Get var values:
eval('global '.$vtitle.'; $vvalue = '.$vtitle.';');
//Get vartype
if( is_array($vvalue) )
{
$vtype = 'Array';
}
elseif( empty($vvalue) )
{
print 'The var "'.$vtitle.'" is empty or doesnt
exist.'."\n";
return TRUE;
}
elseif ( is_string($vvalue) )
{
$vtype = 'String';
}
elseif ( is_integer($vvalue) )
{
$vtype = 'Integer';
}
elseif ( is_resource($vvalue) )
{
$vtype = 'Resource';
}
elseif ( is_object($vvalue) )
{
$vtype = 'Object';
}
elseif ( is_null($vvalue) )
{
print 'The var "'.$vtitle.'" is NULL.'."\n";
return TRUE;
}
else
{
$error = '<b>General Error:</b> PHP says the variable '.$vartitle.'
is "'.gettype($varvalue).'" but can\'t proof this!';
return FALSE;
}
//Print the result:
print 'The var "'.$vtitle.'" is a '.$vtype.' and its value is:
';
eval(($vtype == 'Array') ? 'print_r($vvalue);' : 'print
$vvalue."\n";');
return TRUE;
}
?>
Returns:
The var "$teststring" is a String and its value is: test
The var "$testinteger" is a Integer and its value is: 123
The var "$testarray" is a Array and its value is: Array (
[0] => Test1
[1] => Test2
)
The var "$empty" is empty or doesnt exist.
Syntax Error"A name for a variable, which is not valid.. $)(" is not a valid name for a
var!