PHP Data Types

PHP has built-in Data Types which is divided into three catagories Scalar types,Compound types and Special types. PHP has total eight (8) different data types you can work with.

Scalar types

  • boolean
  • integer
  • float
  • string

Compound types

  • array
  • object

Special types

  • resources
  • NULL

Boolean Type

In PHP the boolean data type is a primitive data type they have only two possible values true and false. This is a fundamental data type and very common in computer programs.

PHP Boolean Type Example

Integer Type

Integer is a whole number. PHP integer is the same as the long data type in C. It is a number between -2,147,483,648 and +2,147,483,647. Integers can be specified in three different notations in PHP. Decimal, hexadecimal and octal. Octal values are preceded by 0, hexadecimal by 0x.

Rules for integers

  • Integer must have at least one digit (0-9)
  • Integer must not have a decimal point
  • Integer can be either positive or negative
  • Integer cannot contain comma or blanks

PHP Integer Type Example

This will produce following result

Floating Point Numbers

Float may contain any decimal number or a number in exponential form.Floating point numbers in PHP can be larger than integers. PHP recognizes two types of floating point numbers. The first is a simple numeric literal with a decimal point and second is a floating-point number written in scientific notation.

PHP Floating Point Type Example

This will produce following result

String Type

String is a series of single characters such as "Hello PHP". Most of working with Web pages is about working with strings. In PHP a string must be delimited by one of four syntaxes: single quotes, double quotes, heredocs, and nowdocs.

PHP String Type Example

This will produce following result

Array Type

In simple word array is complex data type which hold collection of related data elements.Each of the elements can be accessed by an index.

PHP Array Type Example

This will produce following result

Object Type

In simple word object are instances of class definitions. Objects are user defined data types normaly used to define properties/attributes of object. First we need to declear a class of object that can contain properties and methods.

PHP Object Type Example

This will produce following result

Resource Type

Resources are special data types. They hold a reference to an external resource (such as database connections).

NULL Type

NULL is a special type that only have one possible value and that's null. This type is also treated as a language constant in PHP and is case-insensitive. PHP considers all undefined variables to be of type NULL.

PHP NULL Type Example

This will produce following result