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

Data Literals Supported in PHP

This section describes data literals to specify values for different data types: reserved key words; decimal, octal and hexadecimal numbers; floating point number format; single quoted, double quoted, and heredoc strings.

Data literals are ways to provide values for different data types in PHP source code. PHP data literals rules are summarized below:

1. boolean data literals: Data literals for boolean data type are two reserved key words: true and false. Note that true and false are case in-sensitive - TRUE and FALSE are also valid boolean literals.

2. integer data literals: Data literals for integer data type have 3 forms:

  • Decimal form: A sequence of decimal digits, 0 to 9, with the first digit being > 0. Integer data literals in decimal form can be prefixed with or without positive or negative signs. For example: 366, -42, 6371, ...
  • Octal form: A sequence of octal digits, 0 to 7, with the first digit being 0. Integer data literals in octal form can be prefixed with or without positive or negative signs. For example: 0556, -052, 014343, ...
  • Hexadecimal form: 0x followed by a sequence of hexadecimal digits, 0 to 9 and A to F, with the first digit being 0. Integer data literals in hexadecimal form can be prefixed with or without positive or negative signs. For example: 0x16E, -0x2A, 0x18E3, ...

3. float data literals: Data literals for float data type are numeric values represented in signs and decimal digits with a decimal point and or exponential part. For example: 3.14159, 3e8, -1.25e-2, ...

4. string data literals: Data literals for string data type have 3 forms:

  • Singled quoted form: A sequence of 8-bit characters enclosed in a pair of single quotes ''. String data literals in single quoted form are subject to 2 escape sequences (backslash substitutions), \' and \\, to represent ' and \. For example: 'Today\'s Special:', 'Item Price Unit', 'Apples 3.49 kg', ...
  • Double quoted form: A sequence of 8-bit characters enclosed in a pair of double quotes "". String data literals in double quoted form are subject to a different set of escape sequences (backslash substitutions): \", \\, \t, \r, \n, \$, \ddd and \xHH. \ddd is used to represent any character in its octal value. \xHH is used to represent any character in its hexadecimal value. String data literals in double quoted form are also subject to variable substitutions: $variableName. For example: "Today's Special:", "Item\tPrice\tUnit", "Apples\t3.49\t\x6B\x67", "Eggs\t$price\tdoz\145\156", ...
  • Heredoc form: <<< followed by an identifier and a new line, then a sequence of 8-bit characters in one or more lines, and then the same identifier to end the literal. See the example given in the tutorial script below.
$monthDays = array("Jan"=>31,"Feb"=>28,"Mar"=>31);

5. array data literals: There are no data literals for array data type. Arrays are created by using the array constructor, array() function, or the array element notation, $arrayName[$key].

6. object data literals: There are no data literals for object data type. Objects are created by class constructors.

7. resource data literals: There are no data literals for resource data type. Resources are created by using PHP built-in functions.

8. null data literal: Data literal for null data type is 1 reserved key words: null. Note that null is case in-sensitive - NULL is the same as 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 Supported in PHP