PHP Data Types

A PHP data types is the classification of data into a category according to its attributes.
PHP is a loosely typed language, the type of a variable is explicitly set by programmer.
PHP determines the data type at runtime depending on attributes of data supplied.
var_dump is function returns the data type and value of variable.

PHP supports 10 primitive data types that categorized in 3 types:

  1. Scalar types
  2. Compound types
  3. Special types

php data types

Scalar PHP data types

PHP supports following 4 scalar data types:

  • integer
  • float(floating-point number, aka double)
  • string
  • boolean

Compound types

PHP supports following 4 compound data types:

  • array
  • object
  • callable
  • iterable

Special types

PHP supports following 2 special data types:

  • resource
  • NULL

 
Now following is a brief description of each data type with examples.
 

Integer

Integer is number of the set {…,-3,-2,-1,0,1,2,3….}.
Integer can be specified in:

  • binary
  • decimal
  • hexadecimal
  • octal

Binary integer literals are available since PHP 5.4.0.
To use number as hexadecimal(base 16) prefix number with 0x.
To use number as octal(base 8) prefix number with 0.
For using number as binary(base 2) prefix number with 0b.

Example

PHP does not support unsigned integers.
The size of integer is platform-dependent.
A constant PHP_INT_SIZE is used to determine the integer size.
The constant PHP_INT_MAX is used to determine the maximum value.
The constant PHP_INT_MIN is used to determine the minimum value.

Example

 

Float

Floating-point numbers are decimal numbers. They are also known as double or real numbers.
The size of float is platform dependent.
Example

 

String

String is a series of characters.
A string literals can be specified within any single or double quotes.
Example

 

Booleans

This is a simplest data type.
Boolean data type returns true or false.
True or false are case-insensitive.
Boolean data type is used to test conditions.
‘==’ is operator use to test condition which returns boolean.

 

Array

Arrays give you the ability to store not just one value inside a variable, but a whole bunch of values in a single variable.
Following example shows how to declare array data type.

We will discuss array in detail in our separate chapter array of this tutorial.
 

NULL

NULL value represents a variable with no value.
A variable is considered to be null if it is assigned the constant NULL or it has not set any value.

Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.