PHP Resource Type | How the get_resource_type() Works

In this tutorial, I will explain what does mean the PHP resource and we are going to cover all PHP examples regarding this concept to be easily understandable.

Anyway, the PHP resource is referring to all external accesses which are all external resources that have information or data needed to be manipulated through the main source code, such as database connections, files, documents, streams, or sockets.

Before getting started, you have to look at the get_resource_type callback.

Get Resource Type Function

This is a predefined function, used to help developers see the type of resource during their development stage.

Anyway, the get_resource_type takes a resource type as a parameter. And it returns the name of the resource type as a string value.

The pattern of this callback would as the following code.

<?php 
   get_resource_type( $resource );
?>

In the following sections, I am going to cover the common resource’s type using get_resource_type with a PHP example.

Using PHP Resource with Database Connection

In the past and before PHP 5.5, you were able to use the get_resource_type with the SQL connection function – mysql_connect(). because it was returning a resource type. But currently, this function is deprecated and mysqli_connect() is the alternative.

But we could not able to use the get_resource_type with – mysqli because this callback is returning an object data type.

So before PHP5.5, you were able to use the following code.

<?php 
   $mysql= mysql_connect();
   echo get_resource_type( $mysql ); // mysql link
?>

Using PHP Resource with an External File

In the following example, I will use the fopen() predefined function to get the resource value of the external file. Then will pass it into the get_resource_type callback to see what resource type will print.

<?php 
   $file = fopen("text", "w");
   echo $file; // Resource id #5
   echo get_resource_type( $file ); // stream
?>

Wrapping Up

In this tutorial, you understood what does mean PHP resource, get_resource_type and saw examples for database, file, and stream.

Similar Reads

PHP $_SESSION: Secure Your Web Applications in PHP

It’s very important to remember user data for each session when building web applications. This enables a high level of…

How to Connect a MySQL Database to PHP?

Connecting PHP to MySQL is not just a technical step—it is the basic part of how data-driven websites work. From…

Install PHP on Operating System: Ubuntu, Windows, and macOS

In this tutorial, you will learn how to install PHP on your Operating system such as Ubuntu, Windows, or macOS.…

PHP List MongoDB Collections

Sometimes, you may need to list collections with MongoDB in a PHP environment to manage or analyze your database structure.…

PHP file_exists: Check File and Directory Existence

Whether you are just trying to keep your app from crashing or making sure your users’ uploads don’t accidentally overwrite…

PHP Singleton Pattern: How to Use with Examples

The PHP singleton design pattern makes sure that the class has only one instance and provides a global access point…

PHP filter_input: How to Validate Input Securely

User input can be risky. Hackers exploit weak validation to inject malicious data. PHP filter_input() helps you sanitize and validate…

PHP array_merge: Combine Multiple Arrays into One

You have two or more arrays. You want one. That is the problem array_merge solves in PHP. This function lets…

History of PHP: From PHP/FI to Modern Web Development

You use PHP every day if you build websites, but most people do not know where it came from or…

PHP Callable & Callback: Pass a Function to Another

The callback in PHP lets you pass functions as arguments and reuse code. It helps you to control how tasks…

Previous Article

PHP Object | How to Create an Instance of a Class

Next Article

PHP Array: Accessing and Managing Elements

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.