PHP Data Types

PHP | Data Types

.

It is used to express different data types that are associated with the variables or function return's type.

Data Types In PHP

PHP variables are used to hold different data types such as Integer, Floating point number or Float, String, Booleans, Array, Object, resource, and NULL. These are mainly classified into three categories:

  1. Scalar Types (predefined)
  2. Compound Types (user-defined)
  3. Special Types

PHP Scalar Data Types

There is 4 scalar data type in PHP and it contains a single value.

  • boolean: It is used for true or false values.
  • integer: It is used for whole numbers.
  • float: It is used for real numbers.
  • string: It is used for strings of characters.

PHP Compound Data Types

It can hold multiple data and it is categorized into two parts.

  • array: It is used to store multiple data items.
  • object: It is used for storing instances of classes.

PHP Special Data Types

There are 2 special data types in PHP.

  • resource: Database function return variable will be of type resource.
  • NULL: It represents a variable with no value.

PHP Integer Data Type

PHP integers are basically the whole numbers without decimal point such as 1,2,3,-1,-2,0, etc.

Rules For Integer Data Type:

  • An integer does not contain a decimal point.
  • It can be a positive or negative number..
  • Integers can not contain empty spaces.
  • It has at least one digit(0-9).
  • Integer can be expressed as a decimal (base 10), octal (base 8), hexadecimal (base 16), or binary(base2).
  • The range of an integer varry between 2,147,483,648 and 2,147,483,647 i.e., -2^31 to 2^31.

Source Code

Try it now

PHP Float Point Numbers

Floating point numbers ("floats", "doubles" or "real numbers") are numbers with a decimal point or a number in exponential form.

Source Code

Try it now

PHP String Data Type

A string is a finite sequence of characters that is enclosed with single or double-quotes. The string characters might be letters, numbers, special characters, symbols, etc.

String Represented Method

There are four ways to represent a string. These are:

  • Using Single Quote
  • Using Double Quote
  • Using Heredoc Syntax
  • Using Nowdoc Syntax

Source Code

Try it now

PHP Array

PHP array is a variable that holds multiple values in a single variable. There are two ways to represent array:

  • It can be specified by the array() function.
  • It can also be represented with [ and ].

Source Code

Try it now

PHP Objects Data Types

PHP object is basically is an instance of a user-defined class that has the ability to access instance variables and functions. Every object is completely independent of the other because they have their own properties and methods.

Source Code

Try it now

PHP NULL Value

PHP NULL value is a special variable that has no value. It identifies whether a variable is empty or not. We can use the is_null() function to check any value if it is NULL.

Understand The Basic Differences:

When a user has created a variable like $var then a null value is automatically assigned to it. Therefore, keep in mind that $var1=null and $var2="" is not same. Here $var1 represents null value while $var2 represent empty value.

Source Code

Try it now

PHP Resources

It is a special variable that holds the reference of other external resources. Basically, it belongs to the database function.

Source Code

Try it now

Data Type Related Functions in PHP

gettype() – data type getter returns given variables data types.

var_dump() – This function is used to get the data type of an expression.

is_() – There are several variable functions like is_array(), is_null() and etc., to check a variable with respect to specific data type.

settype() – This is the data type setter, used in PHP type conversion.