1. Data types in PHP are used to store various sorts of data or values. PHP provides eight primitive data types, which are further divided into three categories:
PHP Types of data: Types of Scalars
Various data types can be stored in variables, and different data types can accomplish other things.
A string, such as "Hello world!" is a collection of characters.
Any text enclosed in quotations can be used as a string. You have the option of using single or double quotes:
A non-decimal number between -2,147,483,648 and 2,147,483,647 is an integer data type.
At least one digit is required for an integer.
A decimal point cannot be used in an integer.
A positive or negative integer is a number that can be either positive or negative.
Decimal (base 10) notation, hexadecimal (base 16), octal (base 8) notation, and binary (base 2) notation can all be used to specify integers.
In the example below, $x is an integer. The PHP function var dump() returns the data type and value.
A float (a floating-point number) is a decimal or exponential number with a decimal point.
In the example below, $x is a float. The data type and value are returned by the PHP var dump() function:
TRUE or FALSE are the two possible states of a Boolean.
Booleans are frequently used in conditional testing, with $x = true and $y = false. In a subsequent chapter of this course, you'll learn more about conditional testing.
An array is a single variable that holds many values.
$cars is an array in the following example. The data type and value are returned by the PHP var dump() function:
Object-oriented programming is divided into two parts: classes and objects.
An object is an instance of a class, a template for objects.
Individual objects inherit all of the attributes and actions of the class when they are formed, but the properties will have distinct values for each object.
Let's pretend we have a Car class. A car can have characteristics such as model, color, etc. To hold the values of these characteristics, we may construct variables like $model, $ color, and so on.
When separate objects (Volvo, BMW, Toyota, and so on) are formed, they inherit all of the class's attributes and behaviors, but each object's properties will be distinct.
PHP will automatically call the __construct() function you defined when you create an object from a class.
Null is a unique data type with only one possible value: NULL.
The NULL data variant does not have a value associated with it.
When a variable is created without a value, it is given NULL by default.
The value of a variable can likewise be set to NULL to empty it.
|