PHP | Data Types

Data Types defines the type of data a variable can store. PHP allows eight different types of data types. All of them are discussed below. The first five are called simple data types and the last three are compound data types:

  1. Integer : Integers hold only whole numbers including positive and negative numbers, i.e., numbers without fractional part or decimal point. They can be decimal (base 10), octal (base 8) or hexadecimal (base 16). The default base is decimal (base 10). The octal integers can be declared with leading 0 and the hexadecimal can be declared with leading 0x. The range of integers must lie between -2^31 to 2^31.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // decimal base integers
    $deci1 = 50; 
    $deci2 = 654; 
      
    // octal base integers
    $octal1 = 07; 
      
    // hexadecimal base integers
    $octal = 0x45; 
      
    $sum = $deci1 + $deci2;
    echo $sum;
      
    ?>

    chevron_right

    
    

    Output:

    704
    
  2. Double: Can hold numbers containing fractional or decimal part including positive and negative numbers. By default, the variables add a minimum number of decimal places.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    $val1 = 50.85; 
    $val2 = 654.26; 
      
    $sum = $val1 + $val2;
      
    echo $sum;
      
    ?>

    chevron_right

    
    

    Output:

    705.11
    
  3. String : Hold letters or any alphabets, even numbers are included. These are written within double quotes during declaration. The strings can also be written within single quotes but it will be treated differently while printing variables. To clarify this look at the example below.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    $name = "Krishna";
    echo "The name of the Geek is $name \n";
    echo 'The name of the geek is $name';
      
    ?>

    chevron_right

    
    

    Output:

    The name of the Geek is Krishna 
    The name of the geek is $name
    
  4. NULL: These are special types of variables that can hold only one value i.e., NULL. We follow the convention of writing it in capital form, but its case sensitive.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    $nm = NULL;
    echo $nm;    // This will give no output
      
    ?>

    chevron_right

    
    

  5. Boolean: Hold only two values, either TRUE or FALSE. Successful events will return true and unsuccessful events return false. NULL type values are also treated as false in Boolean. Apart from NULL, 0 is also consider as false in boolean. If a string is empty then it is also considered as false in boolean data type.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    if(TRUE)
        echo "This condition is TRUE";
    if(FALSE)
        echo "This condition is not TRUE";
    ?>

    chevron_right

    
    

    Output:

    This condition is TRUE
    
  6. Arrays: Array is a compound data-type which can store multiple values of same data type. Below is an example of array of integers.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    $intArray = array( 10, 20 , 30);
      
    echo "First Element: $intArray[0]\n";
    echo "Second Element: $intArray[1]\n";
    echo "Third Element: $intArray[2]\n";
          
    ?>

    chevron_right

    
    

    Output:

    First Element: 10
    Second Element: 20
    Third Element: 30
    

    We will discuss all about arrays in details in further articles.

  7. Objects: Objects are defined as instances of user defined classes that can hold both values and functions. This is an advanced topic and will be discussed in details in further articles.
  8. Resources: Resources in PHP are not an exact data type. These are basically used to store references to some function call or to external PHP resources. For example, consider a database call. This is an external resource.
    We will discuss about resources in details in further articles.

This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : VishalPal



Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.