PHP Data Types

For constructing variables, PHP supports various data types.

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 figures. 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

Boolean is the simplest datatype. It holds two values, TRUE(1) or FALSE(0). It uses 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 type is constructed by grouping multiple primitive data types.

Array

An array is a compound data type that can store multiple values of the same data type in a single variable. It is an indexed collection of data values; each index of an array is unique. The indexing of the array starts with 0.

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, an object is defined 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 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 refers to external resource data like file resource, database resource. Resource variables typically hold special handlers to opened files and database connections.

Example

<?php

    //open a file on read mode   

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

    Var_dump($handle);

    echo "<br>";

    //How to connect to the MySQL database using PHP

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

    var_dump($link);

?>

NULL

In PHP, null represents the empty variable. 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 learned about PHP data types and how to use them. The data type determines the operations that we can perform on it.

We hope we have given you all a clear idea about a data type and its importance, its types, and functions. Do check out further blogs on the different modules of PHP. Make sure to check out the previous blogs if you face any difficulty with the basics. Happy Learning!

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