note 41508 added to function.gettype
| From: | MC_Breit at phpws dot org | Date: | Tue, 13 Apr 2004 00:06:07 +0000 |
| Subject: | note 41508 added to function.gettype | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-68050@lists.php.net to get a copy of this message | ||
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!
----
Manual Page -- http://www.php.net/manual/en/function.gettype.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+41508
Delete -- http://master.php.net/manage/user-notes.php?action=delete+41508&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+41508&report=yes
Search -- http://master.php.net/manage/user-notes.php