Vaidikalaya

PHP Data Types


Variables can store different types of data, so the data types define the type of data a variable can store. PHP allows 8 different types of data types. All of them are discussed below.

  1. Integer
  2. Float
  3. String
  4. Boolean
  5. Array
  6. Objects
  7. NULL
  8. resource


1. Integer

An integer is any number including 0, positive numbers, and negative numbers. It can never be a fraction, a decimal, or a percent. So it holds only whole numbers, i.e., numbers without fractional parts or decimal points. And the range of integers must lie between -2^31 to 2^31.

Rules for integers:

  • An integer must have at least one digit.
  • An integer must not have a decimal point.
  • An integer 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
<?php
    $a = 34;  
    $b = 0243;  
    $c = 0x45;  
    echo "Decimal number: ".$a. "\n";  
    echo "Octal number: ".$b. "\n";  
    echo "HexaDecimal number: ".$c. "\n";  
?>

2. Float
A floating-point number is a number with a decimal point. Unlike integers, it can hold numbers with a fractional or decimal point, including a negative or positive sign.
Example
<?php
    $x = 10.365;
    echo $x;  
?>

3. String
A string is a data type that is used to represent text rather than numbers. A string is a sequence of characters and can contain letters, numbers, symbols, and even spaces. String values must be enclosed either within single quotes or in double-quotes.

Note: Single and double quotes both are treated differently. Singly quoted strings are treated almost literally, whereas doubly quoted strings replace variables with their values as well as specially interpreting certain character sequences.

Example
<?php
    $name = "Ram";
    echo "My name is $name \n";
    echo 'My name is $name';  
?>

4. Boolean

Booleans are the simplest data type. 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. You will learn more about conditional testing in a later chapter of this tutorial.

Example
<?php
    $x = true;
    $y = false;
?>

5. Array

A PHP array stores multiple values in one single variable:

Example
<?php
    $fruits=["Apple", 'Orange', 'Banana'];
    print_r($fruits);
?>

6. Objects

Classes and objects are the two main aspects of object-oriented programming. So Objects are the instances of user-defined classes that can store both values and functions. When the objects are created, they inherit all the properties and behaviors from the class, having different values for all the properties.

Example
<?php
    class car{
        function model(){
            $model_name="MG Hector";
            echo "Car Model: ".$model_name;
        }
    }
    $obj=new car();
    $obj->model();
?>

7. NULL

Null is a special data type that can have only one value i.e., NULL. If a variable is created without a value or no value, it is automatically assigned a value of NULL. It is written in capital letters.

Example
<?php
    $name=NULL;
    echo $name;       // this will return no output
    var_dump($name); // this will return data type
?>