Core PHP

Literals

A literal is a constant value that is represented in code. Most of them have an obvious meaning, but literals from langauge to language and can sometimes be tricky understand for novice programmers.

The program below demonstrates literal values for five different data types: integers, floating-point numbers, strings, booleans, and null. Each example is written in two consecutive lines of code: One line of code gives literal as you would write it in the code and the type that it is assigned. Then we output the value so that we can see the value as the program would write it back to us.

Each data type is given with its representative literal values. Integer literals can take on values between roughly plus or minus 2 billion and can be written in octal, deciaml, or hexadecimal, as shown below; we can also append a sign designation. Floating-point numbers have the type double and can designated with various forms of scientific notation or as number that contains a decimal. Also, any number outside the range of intger values is automatically converted to be a double. String literals can contain all alphanumeric characters and can represent all ASCII characters via the appropriate escape sequence with an octal or a hexadecimal number; additional characters can also be represent by escape sequences. Boolean literals can be true or false. The null type can only have one value, namely null, which can be represented with any combination of uppercase or lowercase letters.

Literals.php

<?php

echo "<b>Integer Literals:</b><br />";
echo "The type of the literal 0112 is " . gettype(0112) . "<br />";
echo "Its value in decimal is " . 0112 . "<br />";
echo "The type of the literal 74 is " . gettype(74) . "<br />";
echo "Its value in decimal is " . 74 . "<br />";
echo "The type of the literal 0x4A is " . gettype(0x4A) . "<br />";
echo "Its value in decimal is " . 0x4A . "<br />";
echo "The type of the literal +74 is " . gettype(+74) . "<br />";
echo "Its value in decimal is " . +74 . "<br />";
echo "The type of the literal -74 is " . gettype(-74) . "<br />";
echo "Its value in decimal is " . -74 . "<br /><br />";

echo "<b>Floating-point Literals:</b><br />";
echo "The type of the literal 299792458.0 is " . gettype(299792458.0) . "<br />";
echo "Its value is " . 299792458.0 . "<br />";
echo "The type of the literal 2.99792458e8 is " . gettype(2.99792458e8) . "<br />";
echo "Its value is " . 2.99792458e8 . "<br />";
echo "The type of the literal 2.99792458E8 is " . gettype(2.99792458E8) . "<br />";
echo "Its value is " . 2.99792458E8 . "<br />";
echo "The type of the literal 1.166364E-5 is " . gettype(1.166364E-5) . "<br />";
echo "Its value is " . 1.166364E-5 . "<br />";
echo "The type of the literal .00001166364 is " . gettype(.00001166364) . "<br />";
echo "Its value is " . .00001166364 . "<br />";
echo "The type of the literal 10000000000 is " . gettype(10000000000) . "<br />";
echo "Its value is " . 10000000000 . "<br /><br />";

echo "<b>String Literals:</b><br />";
echo "The type of the literal \"XoaX.net\" is " . gettype("XoaX.net") . "<br />";
echo "Its value is " . "XoaX.net" . "<br />";
echo "The type of the literal \"\\130\\157\\141\\130\\56\\156\\145\\164\" is "
. gettype("\130\157\141\130\56\156\145\164") . "<br />";
echo "Its value is " . "\130\157\141\130\56\156\145\164" . "<br />";
echo "The type of the literal \"\\x58\\x6F\\x61\\x58\\x2E\\x6E\\x65\\x74\" is "
. gettype("\x58\x6F\x61\x58\x2E\x6E\x65\x74") . "<br />";
echo "Its value is " . "\x58\x6F\x61\x58\x2E\x6E\x65\x74" . "<br /><br />";

echo "<b>Boolean Literals:</b><br />";
echo "The type of the literal true is " . gettype(true) . "<br />";
echo "Its value is " . true . "<br />";
echo "The type of the literal false is " . gettype(false) . "<br />";
echo "Its value is " . false . "<br /><br />";

echo "<b>Null Literals:</b><br />";
echo "The type of the literal null is " . gettype(null) . "<br />";
echo "Its value is " . null . "<br />";
echo "The type of the literal NuLl is " . gettype(NuLl) . "<br />";
echo "Its value is " . NuLl . "<br /><br />";

?>
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.