Data Types in PHP

Data Types in PHP

All data stored in PHP variables fall into one of eight basic categories, known as data types . A variable ’ s data type determines what operations can be carried out on the variable ’ s data, as well as the amount of memory needed to hold the data.

PHP supports four scalar data types. Scalar data means data that contains only a single value. Here ’ s a list of them, including examples:

Scalar Data Type Description Example
Integer A whole number 15
Float A floating – point number 8.23
String A series of characters “Hello world!”
Boolean Represents either true or false true

As well as the four scalar types, PHP supports two compound types. Compound data is data that can contain more than one value. The following table describes PHP ’ s compound types:

Compound Data Type Description
Array An ordered map (contains names or numbers mapped to values)
Object A type that may contain properties and methods

Finally, PHP supports two special data types, so called because they don ’ t contain scalar or compound data as such, but have a specific meaning:

Special Data Type Description
Resource Contains a reference to an external resource such as a file or database
Null May only contain null as a value meaning the variable explicitly does not contain any value

Resource types are often returned by functions that deal with external applications or files. The type NULL is reserved for variables that have not been initialized(that is, variables that have not yet had a value assigned to them).
You can use PHP’s built-in function gettype() to test the type of any variable. If you place a variable between the parentheses of the function call, gettype() returns a string representing the relevant type. In Below assigns five different data types to a single variable, testing it with gettype() each time. The commentsin the code show you where the script is in the process.

Related Post