PHP Resource Data Type

PHP deals with various types of data like integer, double, string and etc. Resources are also coming under the list of PHP data types. But, unlike other data types,  resource identifiers are acting as a reference or identifier to access resource data provided by a third party external application. These resource data will be return while performing the operations like connecting the database or opening an external file. When we connect the database with the required configuration details like,

$conn = mysqli_connect(localhost,"root","admin","animals");

The function will return a resource type data to be stored into $conn variable.

With reference to the resource created, we can get required list of data items from the database.


In PHP, while working with files, the required file resources will be stored as resource data. For example,

$fp = fopen("index.php",'r');

Now the file resources are stored into the variable $fp by using the fopen() function of PHP to open the file in reading mode. Using this file resource, we can further work with the corresponding file to perform read, write, append and so many file operation on a need basis.

After performing required tasks with resource data, then it should be closed. For example, the database and file resources could be closed as follows.


This PHP code tutorial was published on May 2, 2013.

↑ Back to Top