PHP Data Types

What is PHP Data Types?

PHP Data types are useful to create variables in PHP. There are simple as well as complex data types available in PHP. Each data type in PHP offers different purposes to construct and use.

PHP is a loose type language and you don’t have to mention the type of data. You have to just assign the value and the PHP defines the data type according to the type of value assigned. When you assign a string to the variable, it is a string data type variable. Similarly, it is applicable to other types of values like numeric, float, etc.

How Many Data Types are There in PHP?

There are 8 data types in PHP that you can create. Here is the list of data types in PHP:

  • String
  • Integer
  • Floating Point Numbers or Double
  • Boolean
  • Array
  • Object
  • Null
  • Resource

Let’s find out more about these data types in detail.

Description of PHP Data Types?

PHP String

PHP string is a sequence of characters to assign to a variable. A string can be letters, numbers, and special characters that you have to enclose within the single(‘ ‘) or double quotes (” “) to make it a string.

Output

Hello World!
Welcome to TutorialDeep!

The above example shows the string that contains text content.

PHP Integer

An integer is a non-decimal number that can be either position or negative. It can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8), and binary (base 2).

Output

int(12345)

The above example shows an integer of 5 digits without any decimal point.

PHP Float

PHP float is also called floating point numbers or double. It is the number with a decimal point or in exponential format.

Output

float(1.876)

The above example shows the floating-point number with 3 decimal places with one digit before the decimal.

PHP Boolean

PHP boolean has two possible values that can be either true or false. It can be used for testing purpose to test whether the given condition is true or false.

Output

bool(true)

The above example shows that the given variable is boolean and it is true.

PHP Array

When you want to store multiple values in one single variable, you can use PHP array data type. An array is a collection of data of similar data types to store in a variable. The elements in an array are stored with a unique index or key that are associated with each elements.

Output

array(3) { [0]=> string(7) “Cricket” [1]=> string(7) “Footbal” [2]=> string(9) “Badminton” }

The above example shows the indexed array that contains 3 elements in one single variable. The output shows the array elements with its type in a variable.

PHP Object

An object is an instance of a class in object-oriented programming. A class has properties and functions that are acquired by an object declared for the class. When the object is created, it inherits the properties and behavior of the class.

Output

object(Laptop)#1 (1) { [“color”]=> string(5) “White” }

PHP Null

When a variable stores no value, it is called a NULL variable in PHP. If a variable has no data stored in it, PHP automatically assigned a NULL value to the variable.

Output

NULL

The above example contains a variable that has a NULL value stored in it. When you use var_dump() to the variable, it shows that it is a PHP NULL variable

PHP Resource

PHP resource is a variable that stores an external resource that can be a file or a database call. The below example contains a resource variable that opens an external resource file.

You May Also Like to Read