Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

PHP type resource

A resource is a runtime handle managed by an extension, such as an open stream.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

is_resource($handle)

A resource is a runtime handle managed by an extension, such as an open stream. Its state changes when it is closed, and is_resource returns false for that closed handle. Check failure before use and release a stream with fclose. Resource is not supported as a native scalar type declaration for parameters or returns.

$handle = fopen('php://memory','w+');
if ($handle === false) { throw new RuntimeException('open'); }
$opened = is_resource($handle);
fwrite($handle,'R'); rewind($handle);
$text = stream_get_contents($handle); fclose($handle);
echo json_encode([$opened,$text,is_resource($handle)]);
[true,"R",false]

Examples

<?php
$handle = fopen('php://memory','w+');
if ($handle === false) { throw new RuntimeException('open'); }
$opened = is_resource($handle);
fwrite($handle,'R'); rewind($handle);
$text = stream_get_contents($handle); fclose($handle);
echo json_encode([$opened,$text,is_resource($handle)]);

Result

[true,"R",false]

Sources