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

PHP is_null() function

By using the is_null function, we can check whether the variable is NULL or not. This function was introduced in PHP 4.0.4.

SYNATX:

  1. bool is_null ( mixed $var )  

Parameter

ParameterDescriptionIs compulsory
varThe variable being evaluated.compulsory

Return Type:

The PHP is_null() function returns true if var is null, otherwise false.

Important Note:

We can unset the variable value by using the unset function.

Example 1

<?php  
    $var1 = TRUE;  
    if (is_null($var1))  
        {  
            echo 'Variable is  NULL';  
        }  
        else  
        {  
            echo 'Variable is not NULL';  
        }  
?>  

output

Variable is not NULL

Example 2

<?php  
    $x= 100;  
    unset($x);  
    echo is_null($x);  
?>  

output

1

Example 3

<?php    
    $x = NULL;   
    $y = "\0";  
    is_null($x) ? print_r("True\n") : print_r("False\n");  
    echo "<br/>";  
    is_null($y) ? print_r("True\n") : print_r("False\n");  
?>  

output

Ture

False

PHP Data Types

PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types:

  1. Scalar Types (predefined)
  2. Compound Types (user-defined)
  3. Special Types

PHP Data Types: Scalar Types

It holds only single value. There are 4 scalar data types in PHP.

  1. boolean
  2. integer
  3. float
  4. string

PHP Data Types: Compound Types

It can hold multiple values. There are 2 compound data types in PHP.

  1. array
  2. object

PHP Data Types: Special Types

There are 2 special data types in PHP.

  1. resource
  2. NULL

PHP Boolean

Booleans are the simplest data type works like switch. It holds only two values: TRUE (1) or FALSE (0). It is often used with conditional statements. If the condition is correct, it returns TRUE otherwise FALSE.

Example:

<?php   
    if (TRUE)  
        echo "This condition is TRUE.";  
    if (FALSE)  
        echo "This condition is FALSE.";  
?>  

Output:

This condition is TRUE.

PHP Integer

Integer means numeric data with a negative or positive sign. It holds only whole numbers, i.e., numbers without fractional part or decimal points.

Rules for integer:

  • An integer can be either positive or negative.
  • An integer must not contain decimal point.
  • Integer can be decimal (base 10), octal (base 8), or hexadecimal (base 16).
  • The range of an integer must be lie between 2,147,483,648 and 2,147,483,647 i.e., -2^31 to 2^31.

Example:

<?php   
    $dec1 = 34;  
    $oct1 = 0243;  
    $hexa1 = 0x45;  
    echo "Decimal number: " .$dec1. "</br>";  
    echo "Octal number: " .$oct1. "</br>";  
    echo "HexaDecimal number: " .$hexa1. "</br>";  
?>

Output:

Decimal number: 34
Octal number: 163
HexaDecimal number: 69

PHP Float

A floating-point number is a number with a decimal point. Unlike integer, it can hold numbers with a fractional or decimal point, including a negative or positive sign.

Example:

<?php   
    $n1 = 19.34;  
    $n2 = 54.472;  
    $sum = $n1 + $n2;  
    echo "Addition of floating numbers: " .$sum;  
?>  

Output:

Addition of floating numbers: 73.812

PHP String

A string is a non-numeric data type. It holds letters or any alphabets, numbers, and even special characters.

String values must be enclosed either within single quotes or in double quotes. But both are treated differently. To clarify this, see the example below:

Example:

<?php   
    $company = "Javatpoint";  
    //both single and double quote statements will treat different  
    echo "Hello $company";  
    echo "</br>";  
    echo 'Hello $company';  
?>

Output:

Hello Javatpoint
Hello $company

PHP Array

An array is a compound data type. It can store multiple values of same data type in a single variable.

Example:

<?php   
    $bikes = array ("Royal Enfield", "Yamaha", "KTM");  
    var_dump($bikes);   //the var_dump() function returns the datatype and values  
    echo "</br>";  
    echo "Array Element1: $bikes[0] </br>";  
    echo "Array Element2: $bikes[1] </br>";  
    echo "Array Element3: $bikes[2] </br>";  
?>  

Output:

array(3) { [0]=> string(13) "Royal Enfield" [1]=> string(6) "Yamaha" [2]=> string(3) "KTM" }
Array Element1: Royal Enfield
Array Element2: Yamaha
Array Element3: KTM

You will learn more about array in later chapters of this tutorial.

PHP object

Objects are the instances of user-defined classes that can store both values and functions. They must be explicitly declared.

Example:

<?php   
     class bike {  
          function model() {  
               $model_name = "Royal Enfield";  
               echo "Bike Model: " .$model_name;  
             }  
     }  
     $obj = new bike();  
     $obj -> model();  
?>  

Output:

Bike Model: Royal Enfield

This is an advanced topic of PHP, which we will discuss later in detail.

PHP Resource

Resources are not the exact data type in PHP. Basically, these are used to store some function calls or references to external PHP resources. For example – a database call. It is an external resource.

This is an advanced topic of PHP, so we will discuss it later in detail with examples.

PHP Null

Null is a special data type that has only one value: NULL. There is a convention of writing it in capital letters as it is case sensitive.

The special type of data type NULL defined a variable with no value.

Example:

<?php   
    $nl = NULL;  
    echo $nl;   //it will not give any output  
?>

Output:

JavaScript Boolean Object

JavaScript Boolean object is a member of global objects and a wrapper class. It is used to create a boolean object which holds true or false value, depending upon the value used while creating the Boolean object.

The Boolean object’s true and false values are different from the primitive boolean type’s true and false values.

As already mentioned, it has two values, true and false. The Boolean object returns false when it is passed with values such as 0, -0, an empty string(""), false, nullundefined, or Not a Number(NaN) while creating the Boolean object. Apart from all these values which set the initial value as false for the Boolean object, all other values, even an empty array([]), empty object({}) or the string “false“, will set the initial value for the Boolean object as true.

Creating JavaScript Boolean Object

To create an instance of the Boolean object, we use the new keyword with the Boolean object constructor function, providing a value at the time of creation.

Following is the syntax for it:

let bool = new Boolean(SOME_VALUE);

Depending upon the value passed, the initial value is set as true or false.

Let’s take an example,

// Creating Boolean Object
let boolObj = new Boolean(true);  
document.write(boolObj);

boolObj = new Boolean(false);  
document.write("<br>"+boolObj);

true false

JavaScript Boolean Object False

JavaScript Boolean Object will have the initial value as false if the value provided at the time of object creation is 0-0NaNnullundefined, false, empty string or even if no value is provided because the default value is also false.

let obj1 = new Boolean();
let obj2 = new Boolean(0);
let obj3 = new Boolean(null);
let obj4 = new Boolean('');
let obj5 = new Boolean(false);

document.write(obj1+" "+obj2+" "+obj3+" "+obj4+" "+obj5)

false false false false false

JavaScript Boolean Object True

Apart from the values specified above, for which the initial value of the Boolean object is false, all other values will set the value as true. Let’s take a few examples,

let obj1 = new Boolean(true);
let obj2 = new Boolean('true');
let obj3 = new Boolean('false');
let obj4 = new Boolean('hello');
let obj5 = new Boolean([]);
let obj6 = new Boolean({});

document.write(obj1+" "+obj2+" "+obj3+" "+obj4+" "+obj5+" "+obj6)

true true true true true true

JavaScript Boolean Object vs. Primitive Boolean type

As we have already mentioned that the boolean object and primitive boolean types are different. The Boolean object is a JavaScript object and not a primitive type, but an object type, which can have true or false as its value.

Let’s take an example, where we will see how the Boolean object and the primitive boolean type behave when used in a JavaScript conditional statement expression.

// Boolean object
let obj = new Boolean(false);

// using in if condition 
if(obj)
{
	document.write("It is boolean object"); // executes
}

// Primitive value
let bool = false;
if(bool)
{
	document.write("It is primitive boolean"); // does not execute
}

It is boolean object

In spite of the false value of the Boolean object, the first if statement executes, that is because when we provide an object in the if condition, it is always evaluated as true.

We can get the value of the Boolean objects by using the valueOf() method of the Boolean object and then it will be treated as a norma primitive type true or false value.

Converting Boolean Object to Primitive

We can use the valueOf() method of the Boolean object for accessing its value,

// Boolean object
let obj = new Boolean(false);

// using the value of Boolean object in condition 
if(obj.valueOf()){
	document.write("It is boolean object");   // does not execute
}
else{ 
	document.write("boolean value is false");   // executes
}

boolean value is false

Methods of Boolean Object

The following are some of the commonly used methods of the Boolean object.

  1. toString(): converts the boolean value into a string and returns the string.
  2. valueOf(): returns the primitive value of a Boolean object.

JavaScript Boolean Object Example

Let’s see another code example for the Boolean object:

<html>
    <head>
        <title>
            Using Boolean Object
        </title>
    </head>
    <body>
        <script type="text/javaScript">
            let bool1 = new Boolean(0);
            let bool2 = new Boolean(8);
            let bool3 = new Boolean(2);
            let bool4 = new Boolean("");
            let bool5 = new Boolean("Hello");
            let bool6 = new Boolean('false');
            let bool7 = new Boolean(null);
            let bool8 = new Boolean(NaN);
            document.write("0 value is boolean :" + bool1+"<br>");
            document.write("8 value is boolean :" + bool2+"<br>");
            document.write("1 value is boolean :" + bool3+"<br>");
            document.write("An empty string is boolean :" + bool4+"<br>");
            document.write("String 'Hello' is   boolean :" + bool5+"<br>");
            document.write("String 'false' is boolean :" + bool6+"<br>");
            document.write("null is boolean :" + bool7+"<br>");
            document.write("NaN is boolean :" + bool8+"<br>");
        </script>
    </body>
</html>