Data Literals Examples for Integer, String and Other Data Types

This section provides a tutorial example on how to use data literals for various data types: TRUE, FALSE, and NULL; decimal, octal and hexadecimal numbers; floating point number format; single quoted, double quoted, and heredoc strings.

To help us understand better on how to use data literals for various data types, I wrote the following tutorial sample script:

```<?php
/* DataLiteralSamples.php
*/

print "\n\n Boolean data literals: ";
\$isGood = TRUE;
print "\n    TRUE: ".\$isGood;

print "\n\n Integer data literals in decimal form: ";
\$leapYearDays = 366;
\$supercoolWater = -42;
print "\n    366: ".\$leapYearDays;
print "\n    -42: ".\$supercoolWater;

print "\n\n Integer data literals in octal form: ";
\$leapYearDays = 0556;
\$supercoolWater = -052;
print "\n    0556: ".\$leapYearDays;
print "\n    -052: ".\$supercoolWater;

print "\n\n Integer data literals in hexadecimal form: ";
\$leapYearDays = 0x16E;
\$supercoolWater = -0x2A;
print "\n    0x16E: ".\$leapYearDays;
print "\n    -0x2A: ".\$supercoolWater;

print "\n\n Float data literals in hexadecimal form: ";
\$pi = 3.14159;
\$lightSpeed = 3e8;
\$gdpGrowth = -1.25e-2;
print "\n    3.14159: ".\$pi;
print "\n    3e8: ".\$lightSpeed;
print "\n    2.7322e1: ".\$gdpGrowth;

print "\n\n String data literals in single quoted form: ";
\$title = 'Today\'s Special:';
\$apples = 'Apples\t3.49\tkg';
\$eggs = 'Eggs\t1.99\tdozen';
print "\n    ".\$title;
print "\n    ".\$apples;
print "\n    ".\$eggs;

print "\n\n String data literals in double quoted form: ";
\$title = "Today\'s Special:";
\$apples = "Apples\t3.49\t\x6B\x67";
\$price = 1.99;
\$eggs = "Eggs\t\$price\tdoz\145\156";
print "\n    ".\$title;
print "\n    ".\$apples;
print "\n    ".\$eggs;

print "\n\n String data literals in heredoc form: ";
\$todaySpecial = <<<EOS
Today's Special:
Item        Price   Unit
Apples      3.49    kg
Eggs        1.99    dozen
EOS;
print "\n".\$todaySpecial;

print "\n\n Null data literals: ";
\$nothing = NULL;
\$void = null;
print "\n    NULL: ".\$nothing;
print "\n    null: ".\$void;
?>
```

If you run this sample script, you should get:

```C:\herong> \php\php DataLiteralSamples.php

Boolean data literals:
TRUE: 1
false:

Integer data literals in decimal form:
366: 366
-42: -42
6371: 6371

Integer data literals in octal form:
0556: 366
-052: -42
014343: 6371

Integer data literals in hexadecimal form:
0x16E: 366
-0x2A: -42
0x18E3: 6371

Float data literals in hexadecimal form:
3.14159: 3.14159
3e8: 300000000
2.7322e1: -0.0125

String data literals in single quoted form:
Today's Special:
Item\tPrice\tUnit
Apples\t3.49\tkg
Eggs\t1.99\tdozen

String data literals in double quoted form:
Today\'s Special:
Item        Price   Unit
Apples      3.49    kg
Eggs        1.99    dozen

String data literals in heredoc form:
Today's Special:
Item        Price   Unit
Apples      3.49    kg
Eggs        1.99    dozen

Null data literals:
NULL:
null:
```