The name itself indicating, data types means type of data.
The values assigned to a PHP variable may be of different data types including simple string and numeric types to more complex data types like arrays and objects.
PHP supports total of eight primitive data types: Integer, Floating point number or Float, String, Booleans, Array, Object, resource, and NULL. These data types are used to construct variables. Now let’s discuss each one of them in detail.
Types of data types in php:
PHP data types are mainly divided into 3 types:
- Scalar Data Types: integer, string, Floating-Point Numbers or Doubles, Boolean
- Composite Data Type: array, object
- Special Data Types: Null, resource
Scalar Data Types: allow us to create variables or constants; which can hold a single value.
Integer: means whole number, i.e. number without floating point
Ex: 122, 2000, -400 etc.
Double: Means decimal or float number, i.e. number containing floating Point.
Ex: 3.142, 9.8, -48.32 etc.
String: indicates a sequence of characters enclosed within pair of double quotations or single quotations i.e. Textual data.
Ex: “Hello world” , ‘Hello JavaScript’ etc.
Boolean: indicates logical or conditional result Boolean data type has two values true and false value.
Note:
integers 0 and -0, double 0.0 and -0.0, string with value “, string with value “0”, Boolean false value, an array with zero elements, the special type NULL, and XML objects created from empty tags, are considered as false in PHP.
Example:
PHP Arrays
An array is a variable that can hold more than one value at a time. It is useful to aggregate a series of related items together, for example, a set of country or city names.
An array is formally defined as an indexed collection of data values. Each index (also known as the key) of an array is unique and references a corresponding value.
Example:
PHP Objects
An object is a data type that not only allows storing data but also information on, how to process that data. An object is a specific instance of a class which serve as templates for objects. Objects are created based on this template via the new keyword.
Every object has properties and methods corresponding to those of its parent class. Every object instance is completely independent, with its own properties and methods, and can thus be manipulated independently of other objects of the same class.
Here’s a simple example of a class definition followed by the object creation.
Example:
Special data Types: allow us to make a variable point to some external resource or not to point anywhere.
NULL: indicates nothing.
A variable of type objects points or refers to another memory location; if it should not point to any memory location then we assign a value null to it.
Example:
When a variable is created without a value in PHP like $var; it is automatically assigned a value of null. Many novice PHP developers mistakenly considered both $var1 = NULL; and $var2 = “”; are same, but this is not true. Both variables are different — the $var1 has null value while $var2 indicates no value assigned to it.
PHP Resources
Indicates variable is pointing to an external resource
A resource is a special variable, holding a reference to an external resource.
Resource variables typically hold special handlers to opened files and database connections.
Example:
- Best AI tools for Software Engineers - November 4, 2024
- Installing Jupyter: Get up and running on your computer - November 2, 2024
- An Introduction of SymOps by SymOps.com - October 30, 2024