©
本文檔使用 php中文網手册 發布
[#1] Kubo2 [2015-03-22 11:28:36]
If you want to find out the datatype of a variable, use PHP's native gettype() function instead of various custom implementations.
[#2] coding-kid at hotmail dot com [2010-04-03 08:13:12]
A simple method to check what type input var is:
<code>
class filter{
private $types = array('string'=> array('number'=> '1', 'callback'=> 'is_string'),
'int'=> array('number' => '2', 'callback' => 'is_int'),
'array'=> array('number' => '3', 'callback' => 'is_array'),
'object'=> array('number' => '4', 'callback' => 'is_object'),
'float'=> array('number' => '5', 'callback' => 'is_float'),
'bool'=> array('number' => '6', 'callback' => 'is_bool'),
'resource'=> array('number' => '7', 'callback' => 'is_resource'),
'null'=> array('number' => '8', 'callback' => 'is_null'));
function __construct(){
}
function type($var, $output_type = 'numeric'){
if(is_string($var)){
$var_type = array("string", "1");
} elseif(is_int($var)){
$var_type = array("int", "2");
} elseif(is_array($var)){
$var_type = array("array", "3");
} elseif(is_object($var)){
$var_type = array("object", "4");
} elseif(is_float($var)){
$var_type = array("float", "5");
} elseif(is_bool($var)){
$var_type = array("bool", "6");
} elseif(is_resource($var)){
$var_type = array("resource", "7");
} elseif(is_null($var)){
$var_type = array("null", "8");
}
if($output_type == 'text'){
return $var_type[0];
} else{
return $var_type[1];
}
}
}</code>
[#3] jfrasca at sheerdev dot com [2005-08-31 10:27:37]
I needed a simple function that would reduce any kind of variable to a string or number while retaining some semblance of the data that was stored in the variable. This is what I came up with:
<?php
function ReduceVar ($Value) {
switch (gettype($Value)) {
case "boolean":
case "integer":
case "double":
case "string":
case "NULL":
return $Value;
case "resource":
return get_resource_type($Value);
case "object":
return ReduceVar(get_object_vars($Value));
case "array":
if (count($Value) <= 0)
return NULL;
else
return ReduceVar(reset($Value));
default:
return NULL;
}
}
?>
[#4] skelley at diff dot nl [2001-09-22 16:55:38]
Sorry to say Mykolas, but your definition would not be correct.
isempty() evaluates to true for NULL, 0, "", false or 'not set' for any variable, object etc. that can be set to a value.
isset() evaluates to true if the variable, object etc. exists at all, whether it is 'empty' or not.
Example:
$foo = 0;
isset($foo); //will evaluate to true.
!empty($foo); //will evaluate to false.
unset($foo);
isset($foo); //will evaluate to false.
!empty($foo); //will evaluate to false.