How many Data Types in php
In PHP, there are eight basic data types:
- Integer: A whole number, either positive or negative
- Float: A number with a decimal point, also known as a "floating point number"
- String: A sequence of characters, enclosed in single or double quotes
- Boolean: A value that can be either
true
orfalse
- Array: A collection of values, indexed by keys
- Object: An instance of a class, which can have its own properties and methods
- Resource: A special type that represents a connection to an external resource, such as a database connection or a file handle
- NULL: A special type that represents the absence of a value
PHP also supports several other specialized data types, such as callables, iterators, and generators.
In PHP, there are eight basic data types:
- Integer: A whole number, either positive or negative For example:
$a = 123; // positive integer
$b = -456; // negative integer
$c = 3.14; // positive float
$d = -2.71828 // negative float
3. String: A sequence of characters, enclosed in single or double quotes For example:
$e = 'Hello, world!'; // string in single quotes
$f = "Goodbye, world!" // string in double quotes
true
or false
For example:$g = true; // boolean true value
$h = false; // boolean false value
5. Array: A collection of values, indexed by keys For example:
$i = array(1, 2, 3, 4, 5); // numeric array
$j = array('red', 'green', 'blue'); // associative array
6. Object: An instance of a class, which can have its own properties and methods For example:
class Person {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function getName() {
return $this->name;
}
public function getAge() {
return $this->age;
}
}
$k = new Person('John', 30); // instance of the Person class
7. Resource: A special type that represents a connection to an external resource, such as a database connection or a file handle For example:
$link = mysqli_connect('localhost', 'username', 'password', 'database'); // database connection $file = fopen('data.txt', 'r'); // file handle
$link = mysqli_connect('localhost', 'username', 'password', 'database'); // database connection $file = fopen('data.txt', 'r'); // file handle
8. NULL: A special type that represents the absence of a value For example:
$l = null; // null value