Php 简明教程
PHP – Special Types
PHP 的两种数据类型 resource 和 NULL - 被归类为 special types 。资源类型的对象是指外部资源,例如数据库连接、文件流等。另一方面,NULL 数据类型是一个未分配任何数据的变量。在本章中,我们将详细了解这些类型。
PHP’s two data types – resource and NULL – are classified as special types. An object of resource type refers to external resources like database connection, file streams etc. On the other hand, a NULL data type is a variable without any data assigned to it. In this chapter, we shall learn more about these types.
Resource Type
PHP 程序经常需要与外部环境交互,例如数据库或磁盘文件等。这些在 PHP 中被视为资源。资源是一种特殊的数据类型,它指向任何此类外部资源。PHP 使用相关函数来创建这些资源。例如,fopen() 函数会打开一个磁盘文件,并将其引用存储在资源变量中。
A PHP program often needs to interact with an external environment such as a database, or a disk file etc. These are treated as resources in PHP. Resource is a special data type that refers to any such external resource. PHP uses relevant functions to create these resources. For example, fopen() function opens a disk file and its reference is stored in a resource variable.
PHP 的 Zend 引擎使用引用计数系统。因此,垃圾回收器会自动销毁引用计数为零的资源,并且无需手动释放资源数据类型所使用的内存。
PHP’s Zend engine uses reference counting system. Hence, a resource with zero reference count is destroyed automatically by garbage collector and the memory used by resource data type need not be freed manually.
不同的 PHP 内置函数返回各自的资源变量。随后,PHP 使用它们与相应的外部环境进行交互。例如,fopen() 函数返回一个文件资源,该资源用作文件句柄,并且通过此资源变量来促进对文件的读/写操作。
Different built-in PHP functions return respective resource variables. Subsequently, PHP uses them for interacting with the corresponding external environment. For example, the fopen() function returns a file resource, which acts as a file handle and the read/write operations on the file are facilitated by this resource variable.
下表总结了返回资源变量的不同函数 −
The following table summarizes different functions that return resource variables −
Resource Type |
Built-in functions |
Definition |
Produced |
Sold |
bzip2 |
bzopen() |
bzclose() |
Bzip2 file |
curl |
curl_init() |
curl_close() |
Curl session |
ftp |
ftp_connect(), |
ftp_close() |
FTP stream |
mssql link |
mssql_connect() |
mssql_close() |
Link to Microsoft SQL Server database |
mysql link |
mysql_connect() |
mysql_close() |
Link to MySQL database |
mysql result |
mysql_db_query(), |
mysql_free_result() |
MySQL result |
oci8 connection |
oci_connect() |
oci_close() |
Connection to Oracle Database |
ODBC link |
odbc_connect() |
odbc_close() |
Link to ODBC database |
pdf document |
pdf_new() |
pdf_close() |
PDF document |
stream |
opendir() |
closedir() |
Dir handle |
stream |
fopen(), tmpfile() |
fclose() |
File handle |
socket |
socket_create() |
Socket_close() |
Socket handle |
xml |
xml_parser_create() |
xml_parser_free() |
XML parser |
zlib |
gzopen() |
gzclose() |
gz-compressed file |
zlib.deflate |
deflate_init() |
None() |
incremental deflate context |
zlib.inflate |
inflate_init() |
None() |
incremental inflate context |
PHP 具有 get_resource_type() 函数,该函数返回变量的资源类型。
PHP has get_resource_type() function that returns resource type of a variable.
get_resource_type ( resource $handle ) : string
其中 $handle 是要获取其类型的资源变量。此函数返回一个对应于资源类型的字符串。
where $handle is the resource variable whose type is to be obtained. This function returns a string corresponding to resource type.
还有一个 get_resource_id() 函数,它为给定的资源提供一个整数标识符。
There is also get_resource_id() function an integer identifier for the given resource.
get_resource_id(resource $resource): int
Example
此函数提供了一种类型安全的方式来为给定的资源生成整数标识符。
This function provides a type-safe way for generating the integer identifier for a given resource.
<?php
$fp = fopen("hello.php", "r");
$resource = get_resource_type($fp);
$id = get_resource_id($fp);
echo "The resource type is : $resource The resource ID is : $id";
?>
它将生成以下 output −
It will produce the following output −
The resource type is : stream The resource ID is : 5
NULL type
在 PHP 中,没有值的变量称为 null 数据类型。这样的变量有一个值被定义为 NULL。变量可以显式分配 NULL 或使用 unset() 函数将其值设置为 null。
In PHP, a variable with no value is said to be of null data type. Such a variable has a value defined as NULL. A variable can be explicitly assigned NULL or its value been set to null by using unset() function.
$var=NULL;
可以将其他类型的变量强制转换为 null,尽管从 PHP 7.2 开始已弃用将 null 强制转换为其他类型。在早期版本中,使用 (unset)$var 语法完成强制转换
It is possible to cast variable of other type to null, although casting null to other type has been deprecated from PHP 7.2. In earlier versions, casting was done using (unset)$var syntax
Example
以下示例显示如何将 NULL 分配给变量
The following example shows how to assign NULL to a variable
<?php
$var=NULL;
var_dump($var);
?>
它将生成以下 output −
It will produce the following output −
NULL
Example
以下示例将 null 变量执行到其他主变量 −
The following example performs null variable to other primary variables −
<?php
$var = NULL;
var_dump( (int) $var);
var_dump((float)$var);
var_dump((bool) $var) ;
var_dump( (boolean) $var);
?>
它将生成以下 output −
It will produce the following output −
int(0)
float(0)
bool(false)
bool(false)