Data
Literal
PHP
$booleanVar = true;
$integerVar = 12;
$floatingPointVar = 1.618;
$stringVar = "Hello";
$indexedArrayVar = [1, 2, 3];
$associativeArrayVar = ["name"=>"Sam", "age"=>21];
$nullVar = null;
Literals represent values, like: true
, 12
or "Hello"
.
Literals in PHP can be grouped into these categories:
- Boolean
- Integer
- Floating-point
- String
- Indexed Array
- Associative Array
- Null
$ans1 = 42; // Base10
$ans2 = 0b101010; // Binary
$ans3 = 052; // Octal
$ans4 = 0x2A; // Hexadecimal
There are 4 Integer Literal representations:
- Base10
- Binary (base 2)
- Octal (base 8)
- Hexadecimal (base 16)
All the variables defined here are equal to value 42.
$pi1 = 3.14159; // Normal
$pi2 = 314159E-5; // Scientific
There are 2 Floating-point Literal representations:
- Normal
- Scientific
Both variables defined here are equal.
$name1 = 'sam';
$name2 = "sam";
$name3 = <<<'EOT'
sam
EOT;
$name4 = <<<EOT
sam
EOT;
String is a sequence of characters.
There are 3 String Literal representations:
- Single quoted
- Double quoted
- NowDoc
- HereDoc
The differences between these Literals will be explained in the String topic.
$groceries = ["Milk", "Egg", "Corn"];
Array is an ordered sequence of items.
Here, We are creating an array of 3 elements named groceries
.
Arrays will be discussed in the Array topic.
$groceries = ["Milk"=>1, "Egg"=>6, "Corn"=>2];
Associative Array is a container for key/value pairs, in which keys are unique.
Here, We are creating an associative array of 3 elements named groceries
, which each element is a key/value pair.
Arrays will be discussed in the Array topic.