Php Data Type

Variable can store data of different type and different data type can do different things.



Php supports the data type

1-String

  A string is a sequence of characters like "How are you".

 Example-

    <?php
        
         $x= "how are you";
        echo $x;
        echo"<br>";  
        echo "how are you";

    ?>

2-Integer

  •    A Integer data type a non-decimal number between -2,147,483,648 and +2,147,483,647
  •    Integer must not have a decimal point.
  •    Can be either positive(+) or negative(-).
  •     Integers can be specified in: decimal (base 10), hexadecimal (base 16), octal (base 8), or             binary (base 2) notation.
Example :- $x is an integer. The PHP var_dump() function returns the data type and value:
        
       <?php
     
               $x= 1234545;
               echo $x;
               var_dump($x);
       ?>

3-float(floating point number)

  • A float (floating point number) is a number with a decimal point or a number in exponential form.
Example:- $x is a float. The PHP var_dump() function returns the data type and value:
      
        <?php
     
               $x= 45.86;
               echo $x;
               var_dump($x);
       ?>

4-Boolean

  • A Boolean represents two possible states: TRUE or FALSE.
  • Boolean are often used in conditional testing.
Example:-
       $a=ture;
       $b=false;  

5-Array

  • An array stores multiple values in one single variable.
Example:- $language is an array. The PHP var_dump() function returns the data type and value:

        <?php
     
               $language= array("Php","Java","C","C++");
               echo $language;
               var_dump($language);
       ?>

6-Object

  • An object is a data type which stores data and information on how to process that data.
  • First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods:
Example:-
     <?php
          class Cs {
          function Cs() {
                                         $this->language = "PhP";
                                   }
                            }

           // create an object
          $obj = new Cs();

          // show object properties
          echo $obj->language;    
     ?>

7-Null

  • Null is a special data type which can have only one value: NULL.
  • If a variable is created without a value, it is automatically assigned a value of NULL.
  • Variables can also be emptied by setting the value to NULL.
Example:-

     <?php
          $x = "How are you";
          $x = null;
         var_dump($x);
     ?>

8-Resource

  • The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP.
  • A common example of using the resource data type is a database call.