How to Returns the type of a resource in PHP ?
In PHP, we can use the
get_resource_type() function to return the type of a resource. This function takes a resource as its parameter and returns a string representing the type of the resource.
Here's an example:
// Open a file for reading $file = fopen("example.txt", "r"); // Get the type of the resource $type = get_resource_type($file); // Output the type echo $type; // Outputs: "stream"
In this example, we open a file for reading using the
fopen() function, which returns a resource. We then pass this resource to the
get_resource_type() function to get its type, which is "stream" in this case.
Other examples of resource types in PHP include:
- "curl" for a cURL handle
- "gd" for an image resource created with the GD library
- "mysqli" for a MySQLi database connection
- "pdo" for a PDO database connection
Here's an example of using
get_resource_type() with a cURL handle:
// Initialize a cURL session $curl = curl_init("https://www.example.com"); // Get the type of the resource $type = get_resource_type($curl); // Output the type echo $type; // Outputs: "curl"