PHP Data Types

PHP Data Types

Data types are used to store different types of data or values in variables.

PHP can be categorized data types into 3 types:

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

1.                Scalar Types

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

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

2.                 Compound Types

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

  1. array
  2. object

3.                 Special Types

There are 2 special data types in PHP.

  1. resource
  2. NULL

PHP Boolean

Booleans are the simplest data type that works like a 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:

  1. <?php   
  2.     if (TRUE)  
  3.         echo "This condition is TRUE.";  
  4.     if (FALSE)  
  5.         echo "This condition is FALSE.";  
  6. ?>  

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 a 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:

  1. <?php   
  2.     $dec1 = 34;  
  3.     $oct1 = 0243;  
  4.     $hexa1 = 0x45;  
  5.     echo "Decimal number: " .$dec1. "</br>";  
  6.     echo "Octal number: " .$oct1. "</br>";  
  7.     echo "HexaDecimal number: " .$hexa1. "</br>";  
  8. ?>  

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:

  1. <?php   
  2.     $n1 = 19.34;  
  3.     $n2 = 54.472;  
  4.     $sum = $n1 + $n2;  
  5.     echo "Addition of floating numbers: " .$sum;  
  6. ?>  

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:

  1. <?php   
  2.     $COLLEGE = "SJKPGM";  
  3.     //both single and double quote statements will treat different  
  4.     echo "Hello $COLLEGE";  
  5.     echo "</br>";  
  6.     echo ‘Hello $COLLEGE’;  
  7. ?>  

Output:

Hello SJKPGM
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:

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

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

 PHP object

Objects are the instances of user-defined classes that can store both values and functions. They must be explicitly declared.  An object is a specific instance of a class that serves as templates for objects. Objects are created based on this template via the new keyword.

Every object has properties and methods corresponding to those of its parent class. Every object instance is completely independent, with its own properties and methods, and can thus be manipulated independently of other objects of the same class.

Example:

  1. <?php   
  2.      class bike {  
  3.           function model() {  
  4.                $model_name = "Royal Enfield";  
  5.                echo "Bike Model: " .$model_name;  
  6.              }  
  7.      }  
  8.      $obj = new bike();  
  9.      $obj -> model();  
  10. ?>  

Output:

Bike Model: Royal Enfield

 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 resourcesFor example - a database call. It is an external resource.


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:

  1. <?php   
  2.     $nl = NULL;  
  3.     echo $nl;   //it will not give any output  
  4. ?>  

Post a Comment

0 Comments