Go4Expert Tutorials

Programming And Web Development Tutorials

 
  • Home
  • Tutorials
  • Python
  • PERL
  • ASP.Net
  • C++
You are here: Home / Tutorials / PHP / Datatypes in PHP

Datatypes in PHP

Variables can contain data of different types. Datatypes are an essential feature of any programming language because they allow us to process different types of information. PHP largely supports the following primitive data types:

  • String
  • Integer
  • Float ( Floating point numbers or double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

Strings in PHP

A set of characters like “Example” constitute a string. A string consists of text which is enclosed by quotes. Double or single quotes are used because we need to differentiate a string from the rest of the text in the program. Depending on the type of quote used the string is processed differently.

Example :

Code:

Integers in PHP

Any whole number between -2,147,483,648 and 2,147,483,647 is called as an integer. The datatype which is used to represent an integer is called the Int datatype. PHP requires us to follow some rules while using integer data types :

  • An integer must be a valid whole number between 2,147,483,648 and 2,147,483,647
  • An integer does not have a decimal point and can either be positive or negative.
  • Integers can be specified in 3 formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)

In the following example, $no1 is an integer. The PHP var_dump() function returns the data type and value:

Example:

Code:

Float in PHP

A number which has a decimal point or a number in the exponential form is called a floating point number because of the decimal point which is usually a part of the number. The datatype Float is used for floating point numbers. In the following example var_dump will return the datatype and the value.

Example:

Code:

Boolean Variable

A Boolean variable has only 2 possible values : TRUE or FALSE.

Example:

Code:

Boolean variables are handy while doing conditional testing.

Arrays in PHP

An array is used to store multiple data elements(of the same type) using a single variable and then we can access it using indexes. Arrays are popular because we usually need to store single or multi-dimensional data. The var_dump() function will output the array elements and the data type.

Example:

Code:

Objects in PHP

An object is a data type that consists of data as well as information on how to process the data. An object is the only data type which requires explicit declaration.

The declaration of an object class requires us to use the keyword Class. A class is a structure that consists of methods and properties.

Example:
Code:

PHP NULL value

The NULL datatype is so called because a variable of NULL datatype can only have a value of NULL. A NULL variable is a variable that has no value assigned to it. A variable can be assigned a value of NULL to empty it of its current value.

If a variable is not assigned any value initially it is auto-assigned a value of NULL.

Example:

Code:

Resource Datatype in PHP

The resource datatype enables us to store a reference to functions, external resources, and databases which are required by the program. These references need to be stored because they are frequently required by the program. Eg. A database call can be of the resource datatype.

« Operator Types PHP Variables »

Table of Content

  • PHP – An Introduction
  • PHP5 Installation and Environment Setup
  • PHP Syntax Overview
  • Constants in PHP
  • Operator Types
  • Datatypes in PHP
  • PHP Variables
  • PHP Decision Making
  • PHP Loops
  • PHP Arrays
  • PHP Strings
  • PHP Functions
  • PHP File Inclusion Using Require and Include
  • GET and POST Methods’

© Go4Expert Tutorials 2004 - 2025. All Rights Reserved.