Datatypes in PHP
Generally, the datatypes are used to hold different types of data (or) values. Datatypes specifies the different types of data that are supported in PHP.
PHP supports total eight primitive datatypes :
- Integer
- Float
- String
- Boolean
- Array
- Object
- NULL
- Resource
Note :In PHP, var_dump() function returns the data type and value. Ex: int(123).
☞ Integer :
In PHP, Integers hold whole numbers including positive and negative numbers (... -3, -2, -1, 0, 1, 2, 3, ...). i.e., numbers without decimal point. They can be decimal (base 10), octal (0) or hexadecimal (0x) optionally preceded by + or - sign.
Example: "intdemo.php"
Output :
☞ Float :
A float (floating point number) is a number with a decimal point. It hold numbers containing fractional or decimal part including positive and negative numbers.
Example: "floatdemo.php"
Output :
☞ String :
A string is a sequence of characters which are enclosed with single quotes or double quotes.
Example: "stringdemo.php"
Output :
☞ Boolean :
A boolean datatype hold two possible values either TRUE or FALSE.
Example: "blndemo.php"
Output :
☞ Array :
An array datatype can hold more than one value at a time.In other words, An array is collection of elements or items.
Example: "arydemo.php"
Output :
☞ Object :
In PHP, Object is an instance of user defined class that can store both values and functions.Objects are created based on user defined class via the new keyword
Example: "objdemo.php"
Output :
☞ Null :
In PHP, Null is a special data type that has only one value: NULL, And it is case sensitive you must write in capital letters.The special type of data type NULL defined a variable with no value.
Example: "nulldemo.php"
Output :
☞ Resource :
In PHP, A resource is a special variable, that hold a reference to an external resource such as database connection and opened files.The Resources are not exact data type in PHP
Example: "resdemo.php"
Output :
Note : PHP supports eight primitive data types which are categorized into 3 main types. They are:
- Scalar Types: boolean, integer, float and string.
- Compound Types: array and object.
- Special Types: resource and NULL.