PHP: Resources - Manual
A resource is a special variable, holding a reference to an external resource. Resources are created and used by special functions. See the appendix for a listing of all these functions and the corresponding resource types.
See also the get_resource_type() function.
Converting to resource
As resource variables hold special handles to opened files, database connections, image canvas areas and the like, converting to a resource makes no sense.
Freeing resources
Thanks to the reference-counting system being part of Zend Engine, a resource with no more references to it is detected automatically, and it is freed by the garbage collector. For this reason, it is rarely necessary to free the memory manually.
Note: Persistent database links are an exception to this rule. They are not destroyed by the garbage collector. See the persistent connections section for more information.
Found A Problem?
mrmhmdalmalki at gmail dot com ΒΆ
11 days ago
The resource is not a type you can create; it is a PHP internal type that PHP uses to refer to some variables.
For example,
You have a number 5.5, which is a Float, and you can convert it to an int type, but you cannot convert it to a resource.
A resource type is used for external resources, like files or database connections, as shown below:
<?php
// a normal variable
$file_resource = fopen("normal_file.txt", "r");
// after we assigned an external file to that variable using the function fopen,
// PHP starts dealing with the file, and since it is an external resource,
// PHP makes the type of the variable a resource that refers to that operation
var_dump($file_resource);
// the previous var_dump returned bool(false),
// because there was no file that PHP could open.
// and it gave me this warning:
// PHP Warning: fopen(normal_file.txt): Failed to open stream: No such file or directory
// now I am trying to open an existing file
$file_resource = fopen("existed_file.txt", "r");
// then check the type:
var_dump($file_resource);
// it gives this:
// resource(5) of type (stream)
// now the variable's type is resource
?>