
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Resources
Definition and Usage
In PHP, Resource is a special data type that refers to any external resource. A resource variable acts as a reference to external source of data such as stream, file, database etc. PHP uses relevent functions to create these resources. For example, fopen() function opens a disk file and its reference is stored in a resource variable.
PHP's Zend engine uses reference conting system. As a result, a resource with zero reference count is destroyed automatically by garbage collector. Hence, memory used by resource data type need not be freed manually.
Various types of resources can be handled in a PHP script, with the help of coresponding functions. Following table shows a select list −
Resource Type Name | Created By | Destroyed By | Definition |
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 | | fclose() | 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 |
In this context, PHP has get_resource_type() function that returns resource type of a variable.
Syntax
To declare an object of a class we need to use new statement
get_resource_type ( resource $handle ) : string
where $handle is the resource variable whose type is to be obtained. This function returns a string corresponding to resource type
Following example shows resource type of a disk file
Example
<?php $fp=fopen("test.txt","w"); var_dump($fp); ?>
Output
This will produce following result −
resource(5) of type (stream)
Following example uses get_resource_type() function
Example
<?php $fp = fopen("test.txt", "w"); echo get_resource_type($fp) . "
"; ?>
Output
This will produce following result −
stream
- Related Articles
- People Natural Resources
- People as Resources
- (a) What is meant by inexhaustible natural resources? Name two inexhaustible natural resources.(b)What is meant by exhaustible natural resources? Name any two exhaustible natural resources.
- Acquiring Resources – Best Practices!
- What are Natural Resources?
- What is Natural Resources?
- Conservation of Natural Resources
- How to declare multiple resources in a try-with-resources statement in Java 9?
- Acquiring resources mind your steps
- What are JavaScript language resources?
- Try-with-resources in Kotlin
- What are the water resources?
- (a) What are natural resources? Name some of the important natural resources (b) State two factors that work against an equitable distribution of natural resources.
- What are the best Python resources?
- Top Resources for Java Interview Questions
