PHP data types defines the type of data a variable can store. PHP allows eight different data types that can be categorized further in 3 types:
Scalar Types
Compound Types
Special Types
Scalar Types:
There are four scalar data types in PHP.
Integer
Float/double
String
Boolean
Integer Data type:
Integers are whole numbers including positive and negative numbers without fractional part or decimal point. Integers can be decimal (base 10), octal (base 8) or hexadecimal (base 16). The default base is decimal (base 10). The octal integers can be declared with leading 0 and the hexadecimal can be declared with leading 0x. The maximum value of an integer is platform-dependent.
Floating-point numbers are also known as double or real numbers. They can hold numbers containing fractional or decimal part. The maximum value of a float is platform-dependent. Floating-point numbers are larger than integers.
Strings are letters or any character, even numbers also included. These strings are written within a double quote during declaration. You can written strings within a single but it will be treated differently while printing variables as shown in below example:
Boolean is the simplest data type that holds only two values i.e. true and false. The successful events will return true and unsuccessful events return false.
Note: The NULL values are also treated as false in Boolean. Apart from NULL, 0 is also considered as false in boolean. The empty string is also considered as false in boolean data type.
Compound Types:
There are two compound data types in PHP.
Array
Object
Array Data type:
An array is a variable that can store multiple values at a time. The below example show the use of array:
An object is a data type that stores not only data but also information on how to process that data. Objects are defined as instances of user-defined classes that can hold both values and functions.
NULL is the special data type that used to represent empty variables. If a variable is null, it means the variable does not contain any data or value. NULL is the only possible value of type null, it is case sensitive.
Resources are special data types that are used to store references to some function call or external PHP resources. A example of using the resource data type is a database call. for eg:
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok