Definition and Usage
- PHP Version
- 4+
The is_resource() function checks whether a given value is a resource.
It returns true if the value passed as an argument is a resource; otherwise, it returns false.
The resource type is a data type used when interacting with external resources.
Examples of resources include files, database connections, images, and network connections.
Basic Example
/* File resource */
$file = fopen('test.txt', 'w');
var_dump(is_resource($file)); // bool(true)
/* Database connection resource */
$connection = mysqli_connect("localhost", "username", "password", "database");
var_dump(is_resource($connection)); // bool(true)
/* Image resource (when using the GD library for image creation) */
$image = imagecreate(100, 100);
var_dump(is_resource($image)); // bool(true)
/* Network connection resource */
$network = fsockopen("example.com", 80);
var_dump(is_resource($network)); // bool(true)
Note:
The gettype() function returns the data type of the given value as a string.
Syntax
is_resource(mixed $value): bool
Parameters
$value |
Required. The value to check. |
|---|
Return Values
If the value passed as an argument is a resource, the function returns true; otherwise, it returns false.
Things to Keep in Mind
There are several important points to be aware of when using the is_resource() function.
false is Returned for Closed Resource Variables
The is_resource() function is not a strict type-checking function. It returns false for closed resource variables.
The closed resource indicates that a resource previously used for interacting with an external resource has now been closed. This means the closed resource is no longer active and cannot be used.
A closed resource can be understood through the following code example:
/* Opening a file */
$fp = fopen('test.txt', 'w');
var_dump(is_resource($fp)); // bool(true)
/* Closing the file handle */
fclose($fp); // The resource is now closed
var_dump(is_resource($fp)); // bool(false)
As shown in this code example, is_resource() returns false for closed resource variables.
To identify a closed resource, use the gettype() function. This function returns the string 'resource (closed)' for closed resources.
/* Opening a file */
$fp = fopen('test.txt', 'w');
/* Closing the file handle */
fclose($fp);
echo gettype($fp); // 'resource (closed)'
Preventing Memory Leaks
When using the is_resource() function to verify the validity of a resource, the resource must be properly released after use. Failure to do so can result in performance overhead. When a resource is no longer needed, you must prevent memory leaks by using fclose() (for file handles), mysqli_close() (for MySQL connections), or other appropriate functions to release the resource.
Practical Examples
The is_resource() function is convenient and useful in the following scenarios.
Verifying the Validity of a Resource Variable
When handling external resources in PHP, such as file handles, database connections, image resources, or network connections, you can verify whether the variable is a valid resource.
/* Opening a file */
$file = fopen('test.txt', 'r');
if (is_resource($file)) {
// File processing code
} else {
echo 'The file is invalid.';
}
/* Closing the file handle */
fclose($file);
Verifying the Resource Type Using xml_parser_create()
When dealing with various types of resources, you can use the is_resource() function to validate them.
/* Creating an XML parser */
$xml_parser = xml_parser_create();
if (is_resource($xml_parser)) {
// Parsing can be performed only if the XML parser is a valid resource
xml_parse($xml_parser, "<tag>content</tag>", true);
// Process results
xml_parser_free($xml_parser);
} else {
echo 'The XML parser resource is invalid.';
}
- The above examples demonstrate the basic usage of the
is_resource()function. - Since resource creation may fail depending on system conditions, it is a best practice to validate the resource before performing operations.
- It is important to close the resource after completing the operations using it.
- Failure to close the resource may result in memory leaks.
References
See also
- PHP gettype() Function – Get the Data Type of a Variable as a String
- PHP is_int() Function – Checking Whether a Value Is an Integer
- PHP is_float() Function – Checking Whether a Value Is a Float
- PHP is_numeric() Function – Checking Whether a Value Is a Number or a Numeric String
- PHP is_string() Function – Checking Whether a Value Is a String
- PHP is_array() Function – Checking Whether a Value Is an Array
- PHP is_bool() Function – Checking Whether a Value Is a Boolean
- PHP is_object() Function – Checking Whether a Value Is an Object
- PHP is_null() Function – Checking Whether a Value Is NULL
- PHP isset() and empty() Functions – Concepts, Usage, and Key Differences
- PHP Superglobals – Simple and Essential Usage Guide