CXLIX. Variable 变量函数

简介

有关变量行为方式的信息请查看本手册语言参考部分的变量条目。

需求

要编译本扩展模块不需要外部库文件。

安装

本函数库作为 PHP 内核的一部分,不用安装就能使用。

运行时配置

这些函数的行为受 php.ini 的影响。

表格 1. 变量配置选项

名称默认值可修改范围更新记录
unserialize_callback_funcNULLPHP_INI_ALL自 PHP 4.2.0 起可用。
有关 PHP_INI_* 常量进一步的细节与定义参见附录 G

以下是配置选项的简要解释。

unserialize_callback_func string

如果解串行器发现有未定义类要被实例化,将会调用 unserialize() 回调函数(用该未定义类名作为参数)。如果指定函数不存在,或者此函数没有包含/实现该未定义类,则显示警告。所以仅在确实需要实现这样的回调函数时才设置该选项。

参见 unserialize()

资源类型

本扩展模块未定义任何资源类型。

预定义常量

本扩展模块未定义任何常量。

目录
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_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
jfrasca at sheerdev dot com
01-Sep-2005 01:27
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;
   }
}
?>
skelley at diff dot nl
23-Sep-2001 07:55
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.
tapken at engter dot de
05-May-2001 12:41
This function will return a nice tree-view of an array. It's like var_dump but much prettier :-)
Very useful to analyze an array while debugging.
function parray($array,$prep = '') {
/* (c) by Roland Tapken <tapken@engter.de> */
$prep = "$prep|";
while(list($key,$val) = each($array)) {
$type = gettype($val);
if(is_array($val)) {
$line = "-+ $key ($type)\n";
$line .= parray($val,"$prep ");
} else {
$line = "-&gt; $key = \"$val\" ($type)\n";
}
$ret .= $prep.$line;
}
return $ret;
}

Example:
$array = array("test",2,array("foo" => "bar"), 4.23);
echo "<pre>";
echo parray($array);
echo "</pre>";
This will print:
|-> 0 = "test" (string)
|-> 1 = "2" (integer)
|-+ 2 (array)
| |-> foo = "bar" (string)
|-> 3 = "4.23" (double)