Special Types

There are 2 special data types in PHP

  1. Resource
  2. Null

Resource Data type:

It refers the external resources like database connection, FTP connection, file pointers, etc. In simple terms, a resource is a special variable which carrying a reference to an external resource.

Example 1

<?php  
    $conn = ftp_connect("127.0.0.1") or die("Could not connect");  
    echo get_resource_type($conn);  
?>  

output

FTP Buffer

Example 2

<?php  
    $conn= ftp_connect("127.0.0.1") or die("could not connect");  
    echo $conn;  
?>  

output

Resource id #2

Example 3

<?php  
    $handle = fopen("tpoint.txt", "r");  
    var_dump($handle);  
    echo "<br>";  
    $conn= ftp_connect("127.0.0.1") or die("could not connect");  
    var_dump($conn);  
?>  

output

resource(3, stream)

resource(4, FTP Buffer)


Null Data Type:

A variable of type Null is a variable without any data. In PHP, null is not a value, and we can consider it as a null variable based on 3 condition.

  1. If the variable is not set with any value.
  2. If the variable is set with a null value.
  3. If the value of the variable is unset.

Example 1

<?php  
       
    $empty=null;  
var_dump($empty);  
?> 

output

null

Example 2

<?php  
        $a1 = " ";  
     var_dump($a1);  
        echo "<br />";  
        $a2 = null;  
        var_dump($a2);  
?>  

output

strring ‘ ‘ (length=1)

null

Example 3

<?php  
    $x = NULL;  
    var_dump($x);  
    echo "<br>";  
    $y = "Hello javatpoint!";  
    $y = NULL;  
    var_dump($y);  
?>  

output

null

null