DEV Community

MROH
MROH

Posted on • Edited on

Understanding Data types in PHP

Introduction
Data types are fundamental in understanding programming. In this blogpost we discuss the data types used in PHP. We will also use clear code examples to demonstrate how each data type is used. Let’s dive in!
Data types represent the type of data that can be stored and manipulated in variables. PHP supports several basic data types, including:

a photo showing all datatypes in PHP

Integer:
They are whole numbers, without a decimal point, like 6212. They are the simplest type .they correspond to simple whole numbers, both positive and negative. Integers can be assigned to variables, or they can be used in expressions. In PHP, we can declare an integer variable as follows:

code screenshot showing how integers are used in PHP

Float:
Float, also known as a floating-point number or double, represents numbers with decimal points. They like 3.14159 or 49.1. By default, doubles or float print with the minimum number of decimal places needed. As illustrated below.

code screenshot showing how floats are used in PHP

String:
A string represents a sequence of characters, such as letters, numbers, or symbols. Strings can be enclosed in single quotes ('') or double quotes (""). Here's an example:

code screenshot showing how strings are used in PHP

Boolean:
Boolean have only two possible values either true or false. PHP provides a couple of constants especially for use as Booleans: TRUE and FALSE. It is often used in conditional statements and comparisons. Here's an example

code screenshot showing how booleans are used in PHP

Array:
Arrays are sets of data which can be defined in a PHP Script. Arrays can contain other arrays inside of them without any restriction. Below is an example of an indexed array.

code screenshot showing how arrays are used in PHP

Other type of arrays are Associative arrays and Multidimensional arrays.

Object:
Objects are instances of classes and are used in object-oriented programming. An object contains properties (variables) and methods (functions) that define its behavior. Here's a simple example:

code screenshot showing how objects are used in PHP

*Null: *
Null represents the absence of any value. It is often used to indicate that a variable has no assigned value. NULL is a special type that only has one value: NULL. To give a variable the NULL value, simply assign it like this − $my_var = NULL;

Resource
The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP.
A common example of using the resource data type is a database call.

Top comments (0)