Приглашаем посетить
Чулков (chulkov.lit-info.ru)

Literals

Table of Contents
Previous Next

Literals

We've used a lot of literals already in our examples up to this point. PHP understands three basic kinds of literal: textual literals (strings), numeric literals (integers and floating-point numbers), and Boolean literals (true and false).

Textual Literals

You can specify strings in one of three ways: double quotes, single quotes, or ‘here document’ syntax.

When you enclose a string literal with double quotes, PHP parses it looking for several types of special character. It looks for the names of variables and substitutes their value in the process. It looks for backslash characters, and reads the following character in order to determine what to replace the two-character code with. Possible values are:

Value

Meaning

n

Linefeed (LF)

r

Carriage return (CR)

t

Tab

\

Backslash

$

Dollar sign

"

Double-quote

An octal number of up to three digits

The character whose ASCII code corresponds to the octal number

x followed by a hexadecimal number of up to two digits

The character whose ASCII code corresponds to the hexadecimal number

So, the following code:

    <?php
    echo("This text goes\nacross several\nlines\n\t\"and this quotation is
    indented\"");
    ?>

produces:

    This text goes
    across several
    lines
            "and this quotation is indented"

It's worth noting that when you view the output of that PHP script in a web browser, you get this:

This text goes across several lines "and this quotation is indented"

Browsers ignore line breaks and other whitespace characters (tabs, spaces) when rendering. To tell a browser to include a line break you have to use the <br> (or XHTML <br />) tag. Alternatively, you can use the nl2br() function which inserts HTML line breaks before all newline characters in a string.

When you use single quotes, the only escape codes which work are \' and \\, for a single quote character and a backslash respectively. Every other character is reproduced literally. So, if we simply replace the quotation marks in our previous example with single quotes:

    <?php
    echo('This text goes\nacross several\nlines\n\t\"and this quotation is
    indented\"');
    ?>

The output is:

    This text goes\nacross several\nlines\n\t\"and this quotation is indented\"

As you can see, all of the characters are reproduced literally, including the backslashes.

Here Documents

Here documents are a way of including large blocks of formatted text in a string instead of using multiple echo statements. A here document is used like this:

    $hereText=<<<end_delimiter
    All of the text of the here document is included, starting on this line,
    and spanning multiple lines if necessary,
    until, once it finishes, we include the final delimiter on the next line
    end_delimiter;

    echo($hereText);

After the <<< characters, which tell PHP that a here document is about to begin, we supply a final delimiter. This can be any sequence of alphanumeric characters and/or underscores, although it must not begin with a number or an underscore. The text of the here document then begins on the next line. To tell PHP that it has reached the end of the here document, we simply include a line beginning with the final delimiter which we declared at the start.

The text within a here document is interpreted according to the same substitution rules as a double-quoted string, so you can include variables and escape characters.

Numeric Literals

PHP understands both integer and floating-point numbers. Integers can be specified in decimal, octal, or hexadecimal notation:

    <?php
    echo(255);
    echo(0xFF);
    echo(0377);
    ?>

Hexadecimal numbers are identified by the preceding zero followed by an x. Octal numbers simply begin with a zero.

The echo command always outputs integers using decimal notation, even if they were specified as hex or octal, so the above code writes out the number 255 three times.

You can declare negative integers by preceding any of these notations with a minus sign. To declare floating-point numbers, you can use either decimal point or exponential notation. The following are all valid:

    <?php
    echo(0.001);
    echo(1e-3);
    echo(-3.8716E32);
    ?>

The e or E character in the last two versions is regular "exponent".

Boolean Literals

PHP also understands the words true and false, and you can use them in certain operations which require a Boolean value. Like all PHP keywords, these are case insensitive, and you can use true, TRUE, and True interchangeably.


Table of Contents
Previous Next