PHP Tutorials - Herong's Tutorial Examples - v5.18, by Herong Yang
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 2003 (c) HerongYang.com. All Rights Reserved.
*/
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:
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:
Table of Contents
Introduction and Installation of PHP
►PHP Data Types and Data Literals
Data Literals Supported in PHP
►Data Literals Examples for Integer, String and Other Data Types
Overflow of Integer and Float Values
Variables, References, and Constants
Expressions, Operations and Type Conversions
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
Interface with Operating System
Introduction of Class and Object
Integrating PHP with Apache Web Server
Retrieving Information from HTTP Requests
Creating and Managing Sessions in PHP Scripts
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
Functions to Manage Directories, Files and Images
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers
Configuring and Sending Out Emails
Managing PHP Engine and Modules on macOS