| Newsgroups |
php.notes |
| Message-ID |
<[email protected]> |
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
?>
----
Server IP: 206.189.2.9
Probable Submitter: 2001:16a2:f1f1:5600:80e2:9342:a3ed:215a
----
Manual Page -- https://php.net/manual/en/language.types.resource.php
Edit -- https://main.php.net/note/edit/130649
Del: integrated -- https://main.php.net/note/delete/130649/integrated
Del: useless -- https://main.php.net/note/delete/130649/useless
Del: bad code -- https://main.php.net/note/delete/130649/bad+code
Del: spam -- https://main.php.net/note/delete/130649/spam
Del: non-english -- https://main.php.net/note/delete/130649/non-english
Del: in docs -- https://main.php.net/note/delete/130649/in+docs
Del: other reasons-- https://main.php.net/note/delete/130649
Reject -- https://main.php.net/note/reject/130649
Search -- https://main.php.net/manage/user-notes.php