PHP Data Types
Variables can hold several types of data, and different data types can accomplish various functions.
PHP supports the following data types:
- String
- Integer
- Float (floating point numbers - also called double)
- Boolean
- Array
- Object
- NULL
- Resource
PHP String
A string is a collection of characters, such as "Hello, world!".
Any text enclosed in quotations can be used as a string. You have the option of using single or double quotations:
Example :-
";
echo $y;
?>
Output :-
Hello world!
PHP Integer
A non-decimal number between -2,147,483,648 and 2,147,483,647 is considered an integer data type.
Rules for integers
- At least one digit is needed for an integer.
- An integer cannot contain a decimal point.
- The value of an integer might be positive or negative.
- Integer can be defined in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2).
Example :-
PHP Float
A float (a floating-point number) is a decimal or exponentially expressed number.
The float data type can generally hold values up to 1,7976931348623E+308 (depending on platform) with a maximum accuracy of 14 digits.
Example :-
PHP Boolean
The two potential states of a Boolean are TRUE and FALSE.
Example :-
Booleans are often used in conditional testing.
Related Links
PHP NULL Value
Null is a unique data type that has just one possible value NULL
.
A value of NULL is automatically allocated if a variable is created without a value.
Note:- NULL means NOTHING. A null is not equal to anything. It cannot compare with anything like text, numbers, and so on.
Example :- Setting the value of a variable to NULL will also empty it:
";
$x = null;
echo $x;
?>
Output :-
NULL
PHP var_dump() Method
The var_dump()
function is used to find the datatype of a given input value.
Example :-
Output :-
float(5.34)
string(Hello)
boolean(true)
PHP Array
An array holds numerous values in a single variable.
Example :- In the example below $cars is an array. Returns data type and value to PHP var dump():
Output :-
[0]=>
string(5) "Volvo"
[1]=>
string(3) "BMW"
[2]=>
string(6) "Toyota" }
PHP Object
The two basic components of object-oriented programming are classes and objects.
A class is an object template, while an object is a class example.
When creating separate objects (Volvo, BMW, Toyota, etc.) all of the attributes and behaviors of the class belong to them, but each object will have distinct property values.
Let's pretend we have a Car class. A car can have characteristics such as model, color, and so on. To hold the values of these properties, we can define variables like $model
, $color
, and so on.
When you create an object from a class, PHP will call you to the __construct() method.
Example :-
color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}
$myCar = new Car("black", "Volvo");
echo $myCar -> message();
echo "
";
$myCar = new Car("red", "Toyota");
echo $myCar -> message();
?>
Output :-
My car is a red Toyota!
Related Links
PHP Resource
The particular type of resource is a real form of data. It stores a reference to external PHP functions and resources.
A database call is a popular example of how to use the resource data type.