Skip to content

PHP Tutorial Data Types

Resources

Lesson 42 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

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.

index.php
PHP
<?php
$file = fopen("php://memory", "r+");

var_dump(is_resource($file));
echo gettype($file) . "\n";
echo get_resource_type($file);

fclose($file);
Output
OUTPUT
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”.

index.php
PHP
<?php
$file = fopen("php://memory", "r");
fclose($file);

echo gettype($file);
Output
OUTPUT
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.

info
You will use resources mostly for files
The most common resource is the stream from fopen(). File Handling shows how to read and write files.