Introduction: This time we introduce some system functions related to variables in the PHP manual. Below, Tianya explains the most commonly used ones in detail.
empty — Check whether a variable is empty
"", 0, "0", NULL, FALSE, array(), var $var; and objects without any attributes will be considered empty [Tianya Note: This There seems to be a problem? 】
isset — Check whether the variable is set
is_null — Check whether the variable is NULL
The following are the local environment test results: Apache2.2/MySQL5.5/php-5.2.17
//Tianya PHP blog http://blog.phpha.com
error_reporting(0);
class phpha{}
$var1;
$var2 = '';
$var3 = NULL;
$var4 = array();
$var5 = new phpha;
echo empty($var1) ? 'empty | ' : 'not empty | ';
echo is_null($var1) ? 'is_null | ' : 'not is_null | ';
echo isset($var1) ? 'isset' : 'not isset ';
echo '
';
echo empty($var2) ? 'empty | ' : 'not empty | ';
echo is_null($var2) ? 'is_null | ' : 'not is_null | ';
echo isset($var2) ? 'isset' : 'not isset ';
echo '
';
echo empty($var3) ? 'empty | ' : ' not empty | ';
echo is_null($var3) ? 'is_null | ' : 'not is_null | ';
echo isset($var3) ? 'isset' : 'not isset ';
echo '
';
echo empty($var4) ? 'empty | ' : 'not empty | ';
echo is_null($var4) ? 'is_null | ' : 'not is_null | ';
echo isset($var4) ? 'isset ' : 'not isset ';
echo '
';
echo empty($var5) ? 'empty | ' : 'not empty | ';
echo is_null($var5) ? 'is_null | ' : 'not is_null | ';
echo isset($var5) ? 'isset' : 'not isset ';
?>
//Tianya PHP Blog http://blog.phpha.com
The output is as follows:
empty | is_null | not isset
empty | not is_null | isset
empty | is_null | not isset
empty | not is_null | isset
not empty | not is_null | isset
get_resource_type — Returns the resource type
$c = mysql_connect();
echo get_resource_type($c)."n";
// 打印:mysql link
$fp = fopen("foo","w");
echo get_resource_type($fp)."n";
// 打印:file
$doc = new_xmldoc("1.0");
echo get_resource_type($doc->doc)."n";
// 打印:domxml document
?>
gettype — 获取变量的类型
settype — 设置变量的类型
返回的字符串的可能值为:
“boolean”(从 PHP 4 起)
“integer”
“double”(由于历史原因,如果是 float 则返回“double”,而不是“float”)
“string”
“array”
“object”
“resource”(从 PHP 4 起)
“NULL”(从 PHP 4 起)
“user function”(只用于 PHP 3,现已停用)
“unknown type”
import_request_variables — 将 GET/POST/Cookie 变量导入到全局作用域中
将 GET/POST/Cookie 变量导入到全局作用域中。如果你禁止了 register_globals,但又想用到一些全局变量,那么此函数就很有用。
【天涯注】最好别乱用。
intval — 获取变量的整数值
可用于强制类型转换,等同于: intval($num); 或 (int) $num;
$var1 = 10.898;
$var2 = 'hello';
$var3 = '22hello';
echo intval($var1) . '
';
echo intval($var2) . '
';
echo intval($var3);
?>
输出:
10
0 //至于为什么是0请参考PHP类型转换规则
22
is_array — 检测变量是否是数组
is_binary — Finds whether a variable is a native binary string
is_bool — 检测变量是否是布尔型
is_buffer — Finds whether a variable is a native unicode or binary string
is_callable — 检测参数是否为合法的可调用结构
is_double — is_float 的别名
is_float — 检测变量是否是浮点型
is_int — 检测变量是否是整数
is_integer — is_int 的别名
is_long — is_int 的别名
is_numeric — 检测变量是否为数字或数字字符串
is_object — 检测变量是否是一个对象
is_real — is_float 的别名
is_resource — 检测变量是否为资源类型
is_scalar — 检测变量是否是一个标量
is_string — 检测变量是否是字符串
is_unicode — Finds whether a variable is a unicode string
以上全是用来检测变量是否为某一种格式的函数,是则返回TRUE,否则返回FALSE。
print_r — 打印关于变量的易于理解的信息。
var_dump — 打印变量的相关信息
这2个不需要介绍了,调试的时候相当的常用。
serialize — 产生一个可存储的值的表示
unserialize — 从已存储的表示中创建 PHP 的值
//The above function operations are generally called "serialization" and "deserialization"
//It is important to note that serialize() can handle any type except resource.
//Tianya PHP Blog http://blog.phpha.com
//The most common way to serialize an array, and the most common way to cache files is to use this function
$phpha = array('hello' , 'world', 'love');
$phpha_s = serialize($phpha);
$phpha_us = unserialize($phpha_s);
echo $phpha_s;
print_r($phpha_us);
?>
//Output As follows:
a:3:{i:0;s:5:"hello";i:1;s:5:"world";i:2;s:4:"love";}Array
(
[ 0] => hello
[1] => world
[2] => love
)
strval — Get the string value of a variable
$phpha = strval(1);
var_dump($phpha);
?>
//输出如下:
string(1) "1"
unset — 释放给定的变量
//Can be simply understood as "deleting" a variable
//But it will not "delete" a reference to a variable
$blog = 'http://blog.phpha.com' ;
$phpha = & $blog;
echo $blog . '
' . $phpha . '
';
unset($blog);
echo $blog . '< br />' . $phpha . '
';
?>
//The output is as follows:
http://blog.phpha.com
http://blog.phpha.com
//Since $blog has been "deleted", there will be a Notice level warning
Notice: Undefined variable: blog in E:Apache2.2htdocsindex.php on line 35
http://blog.phpha.com
var_export — Output or return a string representation of a variable
This function returns structural information about the variable passed to the function. It is similar to var_dump(), except that The representation returned is valid PHP code.
$phpha = array(
'title' => '天涯PHP博客',
'url' => 'http://blog.phpha.com',
'type' => 'WordPress'
);
var_dump($phpha);
print_r($phpha);
var_export($phpha);
?>
//输出如下:
array(3) {
["title"]=>
string(15) "天涯PHP博客"
["url"]=>
string(21) "http://blog.phpha.com"
["type"]=>
string(9) "WordPress"
}
Array
(
[title] => 天涯PHP博客
[url] => http://blog.phpha.com
[type] => WordPress
)
array (
'title' => '天涯PHP博客',
'url' => 'http://blog.phpha.com',
'type' => 'WordPress',
)
floatval — 获取变量的浮点值
get_defined_vars — 返回由所有已定义变量所组成的数组
以上就是摘自PHP手册[7] – Variable变量函数的内容,更多相关内容请关注PHP中文网(www.php.cn)!