The 5th Annual China PHP Conference

Variable handling 函数

Table of Contents

  • boolval — 获取变量的布尔值
  • debug_zval_dump — Dumps a string representation of an internal zend value to output
  • doubleval — floatval 的别名
  • empty — 检查一个变量是否为空
  • floatval — 获取变量的浮点值
  • get_defined_vars — 返回由所有已定义变量所组成的数组
  • get_resource_type — 返回资源(resource)类型
  • gettype — 获取变量的类型
  • import_request_variables — 将 GET/POST/Cookie 变量导入到全局作用域中
  • intval — 获取变量的整数值
  • is_array — 检测变量是否是数组
  • is_bool — 检测变量是否是布尔型
  • is_callable — 检测参数是否为合法的可调用结构
  • is_double — is_float 的别名
  • is_float — 检测变量是否是浮点型
  • is_int — 检测变量是否是整数
  • is_integer — is_int 的别名
  • is_iterable — Verify that the contents of a variable is an iterable value
  • is_long — is_int 的别名
  • is_null — 检测变量是否为 NULL
  • is_numeric — 检测变量是否为数字或数字字符串
  • is_object — 检测变量是否是一个对象
  • is_real — is_float 的别名
  • is_resource — 检测变量是否为资源类型
  • is_scalar — 检测变量是否是一个标量
  • is_string — 检测变量是否是字符串
  • isset — 检测变量是否设置
  • print_r — 打印关于变量的易于理解的信息。
  • serialize — 产生一个可存储的值的表示
  • settype — 设置变量的类型
  • strval — 获取变量的字符串值
  • unserialize — 从已存储的表示中创建 PHP 的值
  • unset — 释放给定的变量
  • var_dump — 打印变量的相关信息
  • var_export — 输出或返回一个变量的字符串表示
add a note add a note

User Contributed Notes 4 notes

up
1
skelley at diff dot nl
15 years ago
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.
up
-1
Kubo2
2 years ago
If you want to find out the datatype of a variable, use PHP's native gettype() function instead of various custom implementations.
up
-2
jfrasca at sheerdev dot com
11 years ago
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:

<?
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;
    }
}
?>
up
-6
coding-kid at hotmail dot com
7 years ago
A simple method to check what type input var is:

<code>
class filter{
    /*
     * @var: $types (array): holds all the supported datatypes, their number, and the callback that need to be made
     *                             to check if it's valid.
     */
    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(){
       
    }
    /*
     * @desc: This method checks what type the input variable is
     *
     * @param: $var (mixed): The var that will be checked
     *
     * @param: $output_type (string): How the result will be returned
     *
     * @returns: string or int
     */
    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>
To Top