Data Types in PHP 7

Last Updated : May 13, 2021

In this tutorial, we will learn about Data Types which are supported by the PHP programming language. We already discussed how to handle Variables in PHP; we encourage you to review that if you didn’t check it yet. In the PHP program, we don’t have to specify the data type of variable explicitly. It will automatically be defined when we assign a value to them because PHP is a loosely typed language.

Data Types generally represent the type of value that we can store in a Variable. PHP Data Types are majorly classified into three groups which are Scalar, Compound, and Special. We will learn one by one in detail.

Scalar Data Types in PHP

Scalar data types are used to store single values, and these are the most widely used data types in PHP. There are four types available in this category, and please find below the details about them.

PHP Boolean Data Type

A Boolean data type used to store true or false values in a variable. These boolean values are default keywords, and they should not be enclosed with single or double quotes. Please find below the examples,

<?php
$is_user_active = true;
$is_admin = false;
$display_errors = true;

echo var_dump($is_user_active) . '<br>';
echo var_dump($is_admin) . '<br>';
echo var_dump($display_errors);
?>

PHP Integer Data Type

The Integer data type is used to store numbers that can be positive, negative, hexadecimal, and octal values. Integer values should not have spaces, commas between them, and by default, it is considered a positive value. Please find below the examples,

<?php
// decimal positive value
$a = 42;
var_dump($a);

// positive value
$b = +42;
var_dump($b);

// negative value
$c = -4212;
var_dump($c);

// octal value
$d = 052;
var_dump($d);

// hexadecimal value
$e = 0x1A;
var_dump($e);
?>

PHP Float/Double Data Type

Float data types are considered Floating point numbers or Doubles or Real numbers, which are decimal numbers with fractional point values. It can be positive or negative values. Please find below the examples,

<?php
$pi_value = 3.141592653589793238;
var_dump($pi_value);

$apple_stock_value = 125.91;
var_dump($apple_stock_value);

$x_total_wealth = 12.2e5;
var_dump($x_total_wealth);
?>

PHP String Data Type

The PHP String data type can hold a sequence of characters which can be anything, including strings, numbers, or special characters. PHP String has to be enclosed with single quotes or double quotes with necessary escape sequence characters. Also, we can use Doc-based syntax which is Heredoc and Nowdoc. Please find below the examples,

<?php
// single quotes
echo $string = 'This is sample text<br>';
// single quotes with variable in it
echo 'This is sample text with $string<br>';

// double quotes
echo $string = "This is another sample text<br>";
// double quotes with variable in it
echo "This is sample text with $string <br>";

// with escape characters
echo "This isn\"t sample text but original <br>";
echo 'This isn\'t sample text but it"s original <br>';

// Heredoc sample
$text = <<<EOL
This is not a sample
text but
original
EOL;
echo '<pre>'. $text .'</pre>';

// Nowdoc example
$text = <<<'EOL'
This is not a sample
text but
original
EOL;
echo '<pre>'. $text .'</pre>';
?>

In the above section, we learned about Scalar data types in PHP. Let’s see other data types in below.

Compound Data Types in PHP

Compound data types are used to store more than one value in a variable. The values can be the same data type or different data types: numbers, strings, etc. There are four types available in this category in PHP 7, and please find below the details about them.

PHP Array Data Type

The PHP Array data type is used to store multiple or series of values which can be any type. The array values are associated with their key, so usually, the key is used to get their values. Please find below the example,

<?php
//typical examples of an Array
$vowel = array('a', 'e', 'i', 'o', 'u');
echo $vowel[0] .'<br>';
print '<pre>'; print_r($vowel); print '</pre>';
?>

We will learn more about Array and its type in the upcoming chapter of this tutorial series.

PHP Objects Data Type

The PHP Object is a type of data that allows storing data and obtaining information about how that data is processed. PHP Objects specifically created for Classes in PHP, and it is an instance for that Class to access their functions and properties. Please find below the example,

<?php
class Sample {
  function show_message() {
    echo "I am called by Object";
  }
}
$object = new Sample;
$object->show_message();
?>

PHP Objects are created by using new keyword and we will learn more about Classes and their properties later in this tutorial series.

PHP Callable/Callback Data Type

Callable type declarations can represent the PHP Callbacks function. PHP user-defined functions are used as parameters in built-in functions like call_user_func, and this function invoked the specified user-defined function for processing. Please find below the example,

<?php
function callback_function() {
    echo 'This is example of callback function';
}
call_user_func('callback_function');
?>

PHP Iterable Data Type

The PHP Iterable data type is used to set a specific parameter as an iterable variable, indicating that the function requires a set of values for that parameter. If that specific parameter’s value not traversable, then it will throw an error. Please find below the example,

<?php
function numbers(): iterable {
    return [1, 2, 3];
}
print '<pre>'; print_r(numbers()); print '</pre>';
?>

Special Data Types in PHP

Special Data Types holds a value that is not a common case but special. Please find below the details about them.

PHP NULL Data Type

The PHP NULL data type is used to store NULL values. It will not store anything else other than NULL. This kind of variable’s values are case-insensitive that we can assign NULL or null in the program. Please find below the example,

<?php
// assign number
$number = 42;
var_dump($number);

// assign NULL value
$number = NULL;
var_dump($number);
?>

PHP Resource Data Type

PHP Resource data type variables holds the value which provide reference information to the external resources like Database, Files, etc., Please find below the example,

<?php
// MySQL database connection statement
$connection = mysqli_connect("localhost", "root", "password");
var_dump($connection);

// Reading file
$handle = fopen("http://www.test.com", "r");
var_dump($handle);
?>

How to check Data Type in PHP

As we know about all these data types, when we use them or if we want to make sure that we are using correct data types or if we want to check if specific variable has certain data type, then we can use below pre-defined functions. These functions used to check the data type of a variable and return it.

NameDescription
is_int() / is_integer()It checks the value is an integer or not.
is_float() / is_double() / is_real()It checks the value is a floating-point number or not.
is_numeric()It checks the value is numeric or not.
is_string()It checks the value is a string or not.
is_bool()It checks the value is boolean(true or false) or not.
is_array()It checks the variable is an array or not.
is_object()It checks the value is an object or not.
is_null()It checks the value is null or not.
is_callableIt checks the variable’s value can be called as a function or not.
is_resourceIt checks the value is resource or not.

In this tutorial, we learned about different Data Types in PHP 7 and their example usage. We will use these data-type variables throughout our example programs in this tutorial series. Let’s jump into the next chapter.