PHP supports several data types, which are used to store different kinds of values.
Here are the main data types in PHP:
1. Integer:
An integer is a whole number, such as 0, 1, 2, -3, etc. In PHP, integers can be specified in decimal, hexadecimal, or octal notation.
$myInt = 42; // decimal notation
$myInt = 0xFF; // hexadecimal notation
$myInt = 077; // octal notation
2. Float:
A float is a decimal number with a fractional part, such as 3.14, 2.0, etc.
$myFloat = 3.14;
3. String:
A string is a sequence of characters, such as “hello”, “world”, etc. In PHP, strings can be enclosed in single quotes or double quotes.
$myString = 'Hello, world!';
$myString = "Hello, world!";
4. Boolean:
A boolean is a value that is either true or false.
$myBool = true;
$myBool = false;
5. Array:
An array is a collection of values, which can be accessed using an index or a key.
$myArray = array(1, 2, 3); // numeric array
$myArray = array('key1' => 'value1', 'key2' => 'value2'); // associative array
6. Object:
An object is an instance of a class, which has properties and methods.
class MyClass {
public $myProp = 'Hello, world!';
public function myMethod() {
echo $this->myProp;
}
}
$myObj = new MyClass();
$myObj->myMethod();
7. Null:
Null is a special value that represents the absence of a value.
$myVar = null;