PHP variables can have values of a variety of data kinds, from straightforward string and numeric types to more complicated data types like arrays and objects.
Eight primitive data types are supported by PHP in total: integer, floating point number or float, string, boolean, array, object, resource, and NULL. Variables are built using these data types. So let's go into greater detail about each of them.
1. String
2. Integer
3. Float (also called double)
4. Boolean
5. Array
6. Object
7. NULL
8. Resource
Let's discuss the above data types:
String:
Strings are collections of characters, where each character corresponds to a single byte.
A string can be up to 2GB long and contain letters, numbers, and special characters (2147483647 bytes maximum).
The simplest approach to expressing a string is to enclose it in single quotation marks (for example, "Hi String"). Double quotes are also acceptable.
<?php
$first = "Hi String";
$second = 'Hi String';
?>
Strings can be defined as double quotations or single quotations.
Integer:
Integer refers to numerical information with a positive or negative sign. It solely stores whole numbers, which are those without fractional or decimal places.
There are some rules for integer data types:
1. Positive and negative integers are both acceptable.
2. The decimal point cannot be present in an integer.
3. Hexadecimal, octal, and decimal are all valid base types for integers (base 16).
4. A number's range must be between -2,147,483,648 and -2,147,483,647, or from -31 to 2.
Lets show an example of an integer data type
<?php
$value = 1000; // decimal number
var_dump($value);
?>
Float (also called double):
As shown in the example below, floating point numbers (sometimes called "floats," "doubles," or "real numbers") are decimal or fractional numbers.
<?php
$value = 100.25; // float number
var_dump($value);
?>
Boolean:
The simplest data type, which operates like a switch, are booleans. There are only two possibilities: YES (1) or FALSE (0). It frequently appears alongside conditional phrases. It returns TRUE if the condition is true and FALSE otherwise.
<?php $value = 100; // true state if ($value == 100){ echo "This statement is true" } if ($value != 100){ echo "This statement is false" } ?>
Array :
A variable that can store multiple values at once is called an array. A collection of comparable objects, such as a list of nation or city names, can be usefully combined.
A formal definition of an array is an indexed grouping of data items. An array's indexes, which are often referred to as keys, are all distinct and point to the same value.
<?php $size = array("Small", "Medium", "Tall"); var_dump($size); echo "<br>"; $size_number = array( "Small" => "36", "Medium" => "38", "Tall" => "40" ); var_dump($size_number); ?>
Object:
A data type known as an object allows for the storage of both data and instructions on how to process that data. A specific instance of a class that acts as a template for other objects is called an object. By using the new keyword, objects are produced based on this template.
Each object contains attributes and operations that are identical to those of its parent class. Each instance of an object is totally autonomous, has its own set of attributes and methods, and can thus be handled separately from other objects of the same class.
Here is a straightforward illustration of how to define a class and then create an object.
<?php #Class definition class car{ #class properties public $name; #class methods function show($name){ return "This car name is " . $this->name; } } #Create object from car class $car_name = new car('Prado'); var_dump($car_name); #output = This car name is Prado ?>
Null:
The sole possible value for the special data type null is NULL. Since it is case sensitive, it is customary to write it in capital letters. A variable with no value was specified by the special data type NULL.
<?php $value1 = "Hi Null"; $value1 = null; var_dump($value1); #output = Null ?>
Recourse:
In PHP, resources are not a precise data type. They are essentially used to store references to external PHP resources or function calls. Consider making a database call. It is a third-party resource. As this is a more complex PHP topic, we will talk about it in more detail and with examples later.
Hope this article will help you to know about php data types