PHP Data Types

For constructing variables, PHP supports various data types. A Data type is an attribute of data which tells the compiler how the programmer uses the data.

PHP Data Types

This article is all about the PHP Datatypes. Data types represent the type of data associated with PHP variables.

PHP supports eight primitive data types that are divide into three types.

  1. Scalar Data types
  2. Compound Data Types
  3. Special Data Types

Read Also: PHP Variables

Scalar Data Types

Scalar data types contain only a single value for variable references.

Integer

Integers are whole numbers. We can specify integers in decimal (base 10), hexadecimal(base 16) or octal (base 8) notation. Integer means numeric data with a negative or positive sign. On a 32 bit machine, it’s around 2 billion, and 64-bit machines usually have large values.

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

Float/Double

Floating-point numbers are also known as floats, doubles or real figure. Floating-point numbers are decimal or fractional numbers.

Example

<?php   

    $n1 = 19.34;  

    $n2 = 54.472;  

    $sum = $n1 + $n2;  

    echo "Sum of floating numbers: " .$sum;  

?>

Output

Addition of floating numbers: 73.812

String

It holds letters or alphabet, numbers and special characters. String values must enclose within single or double-quotes.

Example

<?php   

    $name = "Suman";  

    //single and double quote statements will treat different  

    echo "Hello $name";  

    echo "</br>";  

    echo 'Hello $name'; 

?>

Output

Hello Suman

Hello $name

Boolean

Booleans are simplest datatypes. It holds two values TRUE(1) or FALSE(0). It uses with Conditional Statements. If the condition is correct, it returns TRUE Otherwise; it returns FALSE.

Example

<?php   

    if (TRUE)  

        echo "This condition is TRUE.";  

    if (FALSE)  

        echo "This condition is FALSE.";  

?>

Output

This condition is TRUE.

Compound Data Types

The compound data types will have a group of data in the same class.

Array

An array is a compound data type which can store multiple values of the same data type in a single variable. It is an indexed collection of data values; each index of array is unique.

Example

<?php

    $fruits = array ("Apple", "Banana", "Mango");  

    var_dump($fruits);

    echo "</br>";  

    echo "Array Element1: $fruits[0] </br>";  

    echo "Array Element2: $fruits[1] </br>";  

    echo "Array Element3: $fruits[2] </br>";  

?>

Output

array(3) 

{ [0]=> string(5) "Apple" [1]=> string(6) "Banana" [2]=> string(5) "Mango" }

Array Element1: Apple

Array Element2: Banana

Array Element3: Mango

Object

In PHP object defines as an instance of user-defined classes. It can hold both values and functions. An object is a datatype that not only allows storing data but also information on how to process the data. All object has its properties and methods corresponding to its parent class. An object is an entirely independent of its features and techniques.

Example

<?php   

     class fruit

     {  

         function name() 

          {  

               $fruit_name = "Apple";  

               echo "Fruit Name: " .$fruit_name;  

           }  

    }  

    $obj = new fruit();  

    $obj -> name();  

?>

Output

Fruit Name: Apple

Special Data Types

Resource

PHP resource datatype refer to external resource data like file resource, database resource. Resource variables typically hold special handlers to opened files and database connections.

Example

<?php

    $handle = fopen (“abc.txt”,r);

    Var_dump($handle);

    echo “<br>”;

    $link = mysqli_connect("localhost", "USERNAME", "PASSWORD", "DATABASE");

    var_dump($link);

?>

NULL

In PHP, the null represents the empty variables. A variable without any data is a type of NULL variable. If we create any variable and do not assign any value to it, it will automatically have Null stored.

Example

<?php

$a = NULL;

echo $a; // It will not give any output

?>

Read More: PHP Constants

Conclusion

We learn about PHP data types and how to use them. The data type determines the operations that we can perform on it.

I hope you have a better understanding of PHP datatypes.

Leave a Reply

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

Read previous post:
php echo and print statements
PHP Echo/Print

Echo and print are both used to print the output data on the screen. The print statement is used as...

Close