PHP Data Types With Examples

Definition: PHP Data Type-

Data Types defines the type of data a variable can store. PHP supports total eight primitive data types:

  1. Integer
  2. Floating
  3. String
  4. Booleans
  5. Array
  6. Object
  7. Resource and
  8. Null Data Type

Integer Data Type:

Rules for integers:

  • The range of integers must lie between -2^31 to 2^31.
  • Integers are whole numbers, without a decimal point (..., -2, -1, 0, 1, 2, ...).
  • An integer must have at least one digit
  • An integer must not have a decimal point
  • An integer can be either positive or negative
  • Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)
Notation Preceded with Example
Decimal 128
Hexadecimal 0x 0x80
Octal 0 0200
Binary 0b 0b10000000

Example:



Result:

 int(123)
 int(-123)
 int(26)
 int(83)


Float Data Type

A float (floating point number) is a number with a decimal point or a number in exponential form.

Floating point is also known as "floats", "doubles", or "real numbers".

It Can hold numbers containing fractional or decimal(2.56, 1.24, 7E-10) part including positive and negative numbers. By default, the variables add a minimum number of decimal places.



Example:


Result:

 float(1.234)
 float(10200)
 float(4.0E-10)


String Data Type

A string is a sequence of characters, like "Welcome to aimtocode!". A string can be any text inside quotes. You can use single or double quotes

A string also supports and hold letters, numbers, and special characters and it can be as large as up to 2GB (2147483647 bytes maximum).



Example:


Result:

 Well come to aimtocode!
 Hope you enjoy learning!
 stay here, learn more


Booleans Data Type

A Boolean Data Type represents two possible states either (1) TRUE or (0) FALSE.

A Successfully done event returns true and unsuccessful event returns false.



Example:


Result:


  bool(true) 



Array Data type

An array is a variable that can hold more than one value at a time. It is useful to aggregate a series of related items together

An Array is also knwon as compound data-type which can store multiple values of same data type.

An Array can be declared in two way.

  • An array can be declared using the array() function.
  • An array can be declared wrapping with [ and ].


Example:


Result:

array(3) { [0]=> string(3) "Red" [1]=> string(5) "Green" [2]=> string(4) "Blue" }
array(3) { ["Red"]=> string(7) "#ff0000" ["Green"]=> string(7) "#00ff00" ["Blue"]=> string(7) "#0000ff" } 


Object Data Type:

An object is a particular instance of a class where it can be a combination of variables, functions, and data structures.

Objects are created based on this template via the new keyword.

Every object instance is completely independent, with its own properties and methods, and thus can be manipulated independently of other objects of the same class.



Example:


Result:


  aimtocode



Resource Data Type:

A resource is a special variable, holding a reference to an external resource, It typically hold special handlers to opened files and database connections.


Example:


Result:


  resource(2) of type (stream) 	



Null Data Type:

Null Data Type represents a variable with no value

Null is a special data type which can have only one value: NULL.

If a variable is created without a value, it is automatically assigned a value of NULL.


Example:

Result:

  NULL