• PHP Video Tutorials

PHP - get_resource_type() Function



Definition and Usage

The get_resource_type() function returns the type of a resource.

Syntax

string get_resource_type ( resource $handle )

Parameters

Sr.No Parameter & Description
1

handle

The evaluated resource handle.

Return Values

  • If the given handle is a resource, this function will return a string representing its type.

  • If the type is not identified by this function, the return value will be the string Unknown.

  • This function will return null and generate an error if handle is not a resource.

Dependencies

PHP 4.0.2 and above.

Example

Following example demonstrates the use of function get_resource_type() with valid resource −

<?php
   $resource = fopen("test.txt", "w");
   echo get_resource_type($resource) . "\n";
?>

Output

This will produce following result (it returns a string representing its type) −

stream

Example

Following example demonstrates the use of function get_resource_type() with released resource −

<?php
   $resource = fopen("test.txt", "w");
   fclose($resource);
   echo get_resource_type($resource) . "\n";
?>

Output

This will produce following result (it returns the string Unknown) −

Unknown

Example

Following example demonstrates the use of function get_resource_type() when handle is null −

<?php
   $resource = null;
   echo get_resource_type($resource) . "\n";
?>

Output

This will produce an error in logs as below −

PHP Warning:   get_resource_type() expects parameter 1 to be resource, null given
php_variable_handling_functions.htm
Advertisements