Understand What are PHP Data Types

In this article, you will understand the PHP data types and how to use the predefined PHP get data type.

Anyway, PHP data types can be categorized into 3 types. Such as:

  • PHP Scalar Data Type
  • PHP Compound Data Type
  • Special Data Type

PHP DATA TYPES

Let's understand each one in detail.

PHP Scalar Data Type

The PHP data types provide us a scalar data type that already has also 4 scalar types such as integer, string, Boolean, floats. Each one has another value type. Let's take each one with an example.

PHP Integer

The PHP integer is a numerical or numbers from a list 4,3,2,1,0,-1,-2,-3,-4.for example.

<?php
    $value = 55;
    $value_2 = 120;
?>

Actually the integer data type has 4 types such as decimal, hexadecimal, octal, and binary. Check the following examples.

<?php
    // => Decimal
    echo 120; // 120

    // => Hexadecimal
    echo 0x5AD; //1453

    // => Octal
    echo 0353; // 235

    // => Binary
    echo 0b101010; // 42
?>

In the next block, I will show you the string data type.

PHP String

The string can be defined as a list of characters that can be passed inside double quotation. The PHP string has 4 kinds, such as. Double-quotation, Single-quotation, Here doc, and Now doc. Let's see examples.

<?php
    // => Double Quotation
    echo "This is a double quotation";

    // => Single Quotation
    echo 'This is a single quotation';

    // => Here Doc
    echo <<<IDENTIFIER
        Heredoc String Type
    IDENTIFIER;

    // => Nowdoc
    echo <<<'EOUT'
        Now doc String
    EOUT;
?>

PHP Float

The PHP float is referring to the point in the numbers, it is a decimal point. Let's see an example.

<?php
    // Float 
    echo 55.12
?>

PHP Boolean

The PHP boolean is a logical value that can be put as a value into the variables. For example.

<?php
    var_dump( true ); // => bool(true)
    var_dump( false ); // => boot(false)
?>

PHP Compound Data Type

This is referring to a group that can be contained many values, such as array, object.

PHP Array

The PHP array is a group or a list that has multiple values grouped and indexed by keys. For example.

<?php
    $array = array( "data" => "value", 10, -5, -10 => 100 );
?>

PHP Object

The PHP object is an instance from the class. It can be written as the following one.

<?php
    class Animals { }
    $cat = new Animals(); // an object or instance from the class
?>

PHP Special Data Type

The special data type has only two types, such as null and resource.

<?php
    // => NULL
    echo null; // => refers to ( no value )

    // => Resource
    fopen("file.txt", "r") or die("You can not open this file");
?>

Use PHP Predefined Function Get Type

To use the PHP get type, you have to pass the value as a parameter inside the gettype(). It returns a string result with the data type name. For example.

<?php
    echo gettype(array()); // => array
    echo gettype(11); // => integer
?>

Conclusion

You understood, what are PHP data types and how to use them with examples.

Also, you learned about how to retrieve the data type name using the PHP predefined function gettype().

Resources 


Similar Articles