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 array_intersect_key Function: How it Works with Examples

The array_intersect_key in PHP compares arrays by keys and returns only the parts that match. It is useful when you…

PHP Comment: How to Write Comments in PHP

let's now dive into PHP comments—these simple but super important integral parts of PHP coding. You can think of comments…

PHP sizeof: How to Count Elements in Arrays with Examples

The sizeof function in PHP exists to help developers know how many items an array holds. Understand the sizeof Function…

PHP strict_types: How the Strict Mode Works

If you want to write good PHP code, strict mode should be on your radar. Strict mode—activated by the command declare(strict_types=1); at…

PHP array_intersect_uassoc: How it Works with Examples

PHP array_intersect_uassoc checks two or more arrays and returns matches. It compares both the values and the keys with a…

PHP MySQL WHERE: How to Filter Data in MySQL

Filtering data is a big part of getting the right information to show up. That is where the WHERE clause…

PHP array_key_first Function: How it Works with Examples

The array_key_first helps you to get the first key of an array directly in PHP. It saves time and removes…

IF-Else: Executes the IF-Else Statement in PHP

Programming is all about choices. Everything you do in code boils down to answering a question: What should happen if…

PHP foreach Loop: How to Access Keys & Values in Arrays

PHP foreach loop came to help work with array data in a simple way. Arrays and objects grew more common…

PHP require_once and require

Essentially, “require” and “require_once” are directives in PHP to include and evaluate a specific file during the execution of a…

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.