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_keys: How to Extract Keys in Arrays with Examples

This guide shows how PHP array_keys finds all keys in an array and how you can filter them by a…

PHP array_map Function: How to Transform Arrays with Examples

PHP added the array_map() function to help you apply one function to each item in an array. Understand the array_map…

PHP Global Variables & Superglobals: A Complete Guide

Variables in PHP are general elements used for data storage and other manipulations. Global variables belong to a particular category…

PHP OOP Programming: A Complete Tutorial

A complex web application needs proper organization and maintenance to keep your code structured. This is where PHP Object-Oriented Programming…

Concatenating Strings in PHP: Tips and Examples

In PHP, string operators, such as the concatenation operator (.) and its assignment variant (.=), are employed for manipulating and…

PHP array_is_list: Check if an Array is a List with Examples

You need the array_is_list function to know if an array works as a list or not in PHP. What is…

PHP Filters: How to Validate and Sanitize User Inputs

User input can be unpredictable and dangerous. A simple form submission might carry malicious code or invalid data and lead…

PHP Type Juggling: How it Works Behind the Scenes

PHP type juggling refers to the dynamic system where the type of a variable is determined by its context in…

PHP $_GET: How to Create Dynamic URLs in PHP?

The PHP $_GET— is a tiny part, but strong in the data processing using the URL. There is a site that…

PHP filter_var: Understand How to Sanitize Input

PHP didn’t have a way to check or clean user input. Developers used scattered code—some wrote custom checks, others used…

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.