A resource is a special value that points to something outside PHP, such as an open file or a connection. You do not use the resource’s content directly. You hand it back to functions that know what to do with it.
<?php
$file = fopen("php://memory", "r+");
var_dump(is_resource($file));
echo gettype($file) . "\n";
echo get_resource_type($file);
fclose($file);
bool(true)
resource
stream
fopen() opened a stream and returned a resource. fclose() closed it.
Think of it as a ticket
A resource is like a cloakroom ticket. The ticket is not your coat, but it lets you get your coat back. The ticket here lets PHP functions find the open file.
What happens after closing
After fclose(), the variable no longer holds a valid resource. PHP calls it a “closed resource”.
<?php
$file = fopen("php://memory", "r");
fclose($file);
echo gettype($file);
resource (closed)
Resources are being replaced by objects
In recent PHP versions, many extensions that used to return resources now return objects. For example, since PHP 8.0, curl_init() returns a CurlHandle object instead of a resource. The way you use them stays the same.
That is why you should check with the specific function (is_resource() for files) and not assume.
You will use resources mostly for files
fopen(). File Handling shows how to read and write files.