PHP: Data Types
PHP supports eight different types of data: integer
, double
(aka float
), string
, boolean
, NULL
, resource
, array
and object
.
Unlike in languages like Pascal or C, variables need not be declared of their types before being assigned a value. We take a look at each of them separately in the below sections.
PHP Type integer
Integers in PHP can be specified in four ways: decimal (base 10), hexadecimal (base 16), octal (base 8) and binary (base 2). Hexadecimal numbers are preceded by 0x
, octal by 0
, and binary by 0b
. A sign, +
or -
, can precede an integer.
<?php
$eighteen = 18; // decimal
$eighteen_hex = 0x12; // hexadecimal
$eighteen_oct = 022; // octal
$eighteen_bin = 0b00010010; // binary
?>
Irrespective of the number system the variables are assigned, echo
prints them in their decimal values. If we print the above variables using echo
, we get their values in decimal
echo $eighteen_hex, ' ', $eighteen_oct, ' ', $eighteen_bin; // 18 18 18
There is a function called gettype()
which returns the type of a variable. Applying the function on all the variables declared above, we get them to be of type integer
echo gettype($eighteen); // integer
echo gettype($eighteen_hex); // integer
echo gettype($eighteen_oct); // integer
echo gettype($eighteen_bin); // integer
The largest integer supported is given by the predefined constant, PHP_INT_MAX
, which is platform-dependent. In 64-bit Linux systems, this value is usually 9223372036854775807. Numbers beyond the bounds of type integer
will be represented as of type double
, which is described in the next section
echo gettype(PHP_INT_MAX); // integer
echo gettype(PHP_INT_MAX+1); // double
The predefined constant PHP_INT_SIZE
gives you the size of an integer in your current PHP build. Its value could be 4 or 8, depending on the interpreter/platform. PHP 7.0.0 included PHP_INT_MIN
, the smallest supported integer, which is usually -2147483648 in 32-bit systems and -9223372036854775808 in 64-bit systems.
PHP Type double
Floating-point numbers include a decimal point. In PHP, floating point numbers are specified as of type double
. They can be preceded by a +
or -
sign. PHP supports the standard scientific notation, and hence a floating-point number can also include an e
or E
to denote exponents (the radix being always 10). For example, take Avogadro's Number 6.02214086 x 1023. It can be represented either as 6.02214086E23
or 6.02214086e23
, the exponent 23
being after the letter E
or e
. Few other examples are shown below
<?php
$pi = 3.141598;
$coulomb = 8.987552e9; // Coulomb's Constant
$G = 6.67408E-11; // Gravitational Constant
?>
The maximum supported floating-point value in 64-bit systems is usually 1.7e+308. Anything beyond that value will give INF
<?php
$out = 1.7e+309; // INF
$overflow = 1.8e+308; // INF
echo $out, ' ', $overflow; // INF INF
?>
PHP Type string
A string is a sequence of characters delimited by quotation marks. Quotation marks can either be single (') or double ("). Therefore, both 'Hello, World!'
and "Hello, World!"
are valid strings in PHP. But there is a difference in the way they are treated. Variables inside single-quoted strings will NOT be interpolated (variable interpolation means replacing variables in string with their values). Only variables within double-quoted strings are interpolated. We illustrate it below
<?php
$wr = 'white rabbit';
$knock = 'Follow the $wr';
$knockknock = "Follow the $wr";
echo $knock; // Follow the $wr
echo $knockknock; // Follow the white rabbit
?>
We see that the variable $wr
inside single-quoted strings assigned to $knock
is not interpolated. But $wr
within double-quoted strings assigned to $knockknock
is interpolated.
PHP Type boolean
A Boolean data type is either true
or false
. Results of comparison operators (like >
, ==
, <
, etc) and conditional statements (like if
) convert to Boolean values. An example involving a comparison operator is shown below. Since the variable $n
, assigned with the value 0, is less than 1, the if
condition evaluates the comparison ($n < 1)
as true
<?php
$n = 0;
if($n < 1) { // this if condition is evaluated as true
echo "Hello, World!"; // Hello, World!
}
?>
Next, we take a look at how the if
conditional statement evaluates data of each type: integer
, double
, string
and NULL
.
For integer
data-types, all values, except 0, are evaluated as true
, including negative integers
<?php
$p = 1; // a positive integer
$n = -1; // a negative integer
$zero = 0;
if($p) { // this if condition is evaluated as true
echo "$p is evaluated as true"; // 1 is evaluated as true
}
if($n) { // this if condition is evaluated as true
echo "$n is evaluated as true"; // -1 is evaluated as true
}
if($zero) { // this if condition fails
}
?>
Similarly, for double
data-types, all values, except 0.0, are evaluated as true
, including negative floating-point numbers
<?php
$p = 1.1; // a positive floating-point number
$n = -1.1; // a negative floating-point number
$zero = 0.0;
if($p) { // this if condition is evaluated as true
echo "$p is evaluated as true"; // 1.1 is evaluated as true
}
if($n) { // this if condition is evaluated as true
echo "$n is evaluated as true"; // -1.1 is evaluated as true
}
if($zero) { // this if condition fails
}
?>
In case of string
data-types, only an empty string ""
and string containing just zero "0"
are evaluated as false
. Both of the below two if
conditions fail
<?php
$empty = "";
$zero = "0";
if($empty) { // if condition fails
}
if($zero) { // if condition fails
}
?>
And lastly, a NULL
is always evaluated as false
<?php
$nihil = NULL;
if($nihil) { // if condition fails
}
?>
Both Boolean values are case-insensitive. They can be assigned in all capital-letters as TRUE
/FALSE
, and still represent the same.
PHP Type NULL
The NULL
data type represent no value, except itself. NULL
is different from an empty string. The isset()
function returns false
on a variable assigned to NULL
but returns true
on a variable assigned to an empty string. Below is an example of using NULL
<?php
$nihil = NULL;
?>
NULL
is case-insensitive. We can assign null
to $nihil
in all small-case in the above example and it will still represent the same.
PHP Type resource
, array
, object
We have separate chapters/pages devoted exclusively to datas of type resource
, array
and object
. Respective links are listed below to learn more about them in detail.
-
array
— php-arrays.html -
object
— php-oops-classes-objects.html