PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

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
* Copyright (c) 2005 by Dr. Herong Yang. http://www.herongyang.com/
*/

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

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

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

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

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:';
$headers = 'Item\tPrice\tUnit';
$apples = 'Apples\t3.49\tkg';
$eggs = 'Eggs\t1.99\tdozen';
print "\n    ".$title;
print "\n    ".$headers;
print "\n    ".$apples;
print "\n    ".$eggs;

print "\n\n String data literals in double quoted form: ";
$title = "Today\'s Special:";
$headers = "Item\tPrice\tUnit";
$apples = "Apples\t3.49\t\x6B\x67";
$price = 1.99;
$eggs = "Eggs\t$price\tdoz\145\156";
print "\n    ".$title;
print "\n    ".$headers;
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:

 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:

Last update: 2005.

Sections in This Chapter

Data Types Supported in PHP

Data Literals Supported in PHP

Data Literals Examples for Integer, String and Other Data Types

Overflow of Integer and Float Values

Dr. Herong Yang, updated in 2009
Data Literals Examples for Integer, String and Other Data Types