What is Data Type In PHP
Data types in PHP are used to store different types of data in a program. PHP supports eight primitive data types: Boolean, Integer, Float, String, Array, Object, NULL, and Resource. Each data type has its own specific purpose and uses cases.
1. Boolean:
A boolean represents two possible states, either TRUE or FALSE.
Example:
$isMarried = FALSE;
echo $isMarried;
// Output:
FALSE
2. Integer:
An integer is a whole number (not a fraction) that can be either positive or negative.
Example:
$age = 24;
echo $age;
// Output:
24
3. Float:
A float (floating point number) is a number with a decimal point or a number in exponential form.
Example:
$pi = 3.14;
echo $pi;
// Output:
3.14
4. String:
A string is a sequence of characters, like “Hello World”.
Example:
$name = "John";
echo $name;
// Output:
John
5. Array:
An array is a special variable, which can hold more than one value at a time.
Example:
$cars = array("Ford", "Toyota", "Honda");
echo $cars[0];
// Output:
Ford
6. Object:
An object is a data type which stores data and information on how to process that data. Example:
class Student {
public $name;
public $age;
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
}
$student = new Student();
$student->setName("John");
echo $student->getName();
// Output:
John
7. NULL:
NULL is a special data type which can have only one value: NULL. Example:
$name = NULL;
echo $name;
8. Resource:
A resource is a special variable, holding a reference to an external resource.
Example:
$file = fopen("test.txt", "r");
echo $file;
// Output:
Resource id #1