PHP Data Types

PHP Data Types is used to store different type of value  like integer, character, string, array, float, decimal, date, boolean etc. in varaibale. We don't need to define any data type for php variable. PHP interpreter will define data type for php variable automatically according to value. So, php is called as loosely typed language.
PHP supports the following data types:

  1. Integer
  2. Boolean
  3. String
  4. Float
  5. Array
  6. Object
  7. NULL
  8. Resource

PHP Integer :

Integer is numeric data with a negative or positive sign between -2,147,483,648 and 2,147,483,647. An integer must not contain decimal point. Integer can be specified in decimal (base 10), octal (base 8), or hexadecimal (base 16).

PHP Code Example
<?php  
$i = 1254;
echo "Integer Value : ".$i;
?>
Output:
Integer Value : 1254

PHP Boolean:

Boolean data type store TRUE or FALSE value.  It is often used with conditional statements. If the condition is correct, it returns TRUE otherwise FALSE.
PHP Code Example
<?php   
       $x=TRUE;
        if ($x){  
            echo "This condition is TRUE.";
         }else{
            echo "This condition is FALSE.";  
        }
    ?> 

Output:
This condition is TRUE.

 

PHP String:

A string is a sequence of characters, like "Learn PHP tutorials". It stores letters or any alphabets, numbers, and even special characters.A string can be specified inside single or double quotes.
PHP Code Example

<?php
$str1 = "Learn PHP tutorials at aryatechno!";
$str2 = 'Learn PHP tutorials at aryatechno!';

echo $str1."<br>";
echo $str2;
?>

Output:
Learn PHP tutorials at aryatechno!
Learn PHP tutorials at aryatechno!

PHP Float:

A floating-point number is a decimal point number including a negative or positive sign.
PHP Code Example
<?php  
$i = 10.365;
$j = 2.456;
$num = $i/$j;
echo "Division of floating numbers: " .$num;
?> 

Output:
Division of floating numbers: 4.2202768729642
 

PHP Array:

An array stores multiple values in one single variable.
PHP Code Example
<?php
$color = array("black","pink","red","green","white");
var_dump($color);
?>

Output:
array(5) { [0]=> string(5) "black" [1]=> string(4) "pink" [2]=> string(3) "red" [3]=> string(5) "green" [4]=> string(5) "white" }

PHP Object:

Object is used to access properties of class in object-oriented programming.
PHP Code Example
<?php   
         class Mobile {  
              function model() {  
                   $model_name = "Samsung";  
                   echo "Mobile Model: " .$model_name;  
                 }  
         }  
         $obj = new Mobile();  
         $obj -> model();  
    ?> 

Output:
Mobile Model: Samsung

PHP NULL:

Null is a special data type which can have only one value: NULL.
A variable of data type NULL is a variable that has no value assigned to it.
Note :  If a variable is created without a value, it is automatically assigned a value of NULL.
PHP Code Example
<?php
$p = NULL;
var_dump($p);
echo $p; // it doesn't show any value;
?>

Output:
NULL


PHP Resource:

The special resource type is not an actual data type. It is used to store some function calls or references to external PHP resources..
A common example of using the resource data type is a database call.

Comments

Leave a Reply

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

12140