PHP data types

Programming

PHP data types- How to define and initialize

PHP data types support a total of eight primitive data types: Integer, Floating-Point or Float, String, Booleans, Array, Resource, Object, and NULL. These types of data are used for making variables. Now let’s talk about each in-depth.

  • Boolean
  • Array
  • Object
  • String
  • Integer
  • Float
  • NULL
  • Resource

Boolean :
A Boolean stands for two possible states: either TRUE (1) or FALSE (0). In conditional testing, booleans are also used. such as if.. else.., loop, etc.

Initializing the boolean value to variables:
$a = false;
$b = true;

Array :
An array is a variable that can store multiple values in a single variable. The formal meaning of an array is the indexed set of data values. Every index of an array (also known as the key) is special and refers to a corresponding value.

How to define :
$cars=array();
$cars=[];

How to Initialization :
$cars=array(“BMW”,”Ferrari”,”Audi”);
or
$cars=[“BMW”,”Ferrari”,”Audi”];

we can define an array using array keyword followed by Parentheses () or we can define an array using square Brackets [].

Object :
An object is a type of data that allows data to be stored as well as information about how to process the data. An object is a particular instance of a class that serves as object templates. Objects are generated using the new keyword, based on this template. In PHP, an object must be explicitly declared. Each object has properties and methods which match those of its parent class. Every object instance is fully autonomous, with its own properties and methods, and can, therefore, be manipulated independently of other objects of the same class. First, we have to define the object type. We use the class keyword for this. A class is a structure that can include properties and methods.

PHP data types

String :
A string is a series of characters, such as “Hello world!”. Any text within quotes can be a string. Single or double quotes can be used. A string can contain letters, numbers, and special characters and can be up to 2 GB in size (maximum of 2147483647 bytes).

$first_name=”Jordan”;
$last_name=’Smith’;

Integer :
An integer data form is a non-decimal number ranging from -2,147,483,648 to 2,147,483,647. Whole numbers, without a decimal point (…, -2, -1, 0, 1, 2, …).Integers can be specified in decimal (base 10), hexadecimal (base 16 – prefixed with 0x) or octal (base 8 – prefixed with 0) optionally followed by a symbol (- or +).

Rules for integer :
An integer should have a minimum of one digit
An integer may either be negative or positive
An integer does not have one decimal point

$number=1234;

Float :
A float (a floating-point number) is a number in exponential form with a decimal point or number.

$x=1.324;

NULL :
Null is a special category of data that can only have one value: the End. A data form variable NULL is a variable that does not have a value assigned to it.

$x=NULL;

Resource :
A resource is a special variable that holds an external resource relation. Usually, resource variables contain special handlers for opened files and connections to databases. A typical example of using the form of resource data is a call to the database. We are not going to speak about the type of resource here, as it’s an advanced subject.