Fork me on GitHub

Strings & Patterns



Functions


Escape Sequences


Matching Strings


Extracting


Formatting



Printf Formatting Specifiers


Replacement


PCRE(Perl Compatible Regular Expressions) – Meta Characters


PCRE – Pattern Modifiers


HEREDOC and NOWDOC

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

As of PHP 5.3.0, it’s possible to initialize static variables and class properties/constants using the Heredoc syntax.

PHP 5.3.0 also introduces the possibility for Heredocs to use double quotes in declarings.

Heredocs can not be used for initializing class properties. Since PHP 5.3, this limitation is valid only for heredocs containing variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

/* More complex example, with variables. */
class foo
{
    var $foo;
    var $bar;

    function foo()
    {
        $this->foo = 'Foo';
        $this->bar = array('Bar1', 'Bar2', 'Bar3');
    }
}

$foo = new foo();
$name = 'MyName';

echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
?>

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc

Unlike heredocs, nowdocs can be used in any static data context. The typical example is initializing class properties or constants

A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc.

A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. All the rules for heredoc identifiers also apply to nowdoc identifiers, especially those regarding the appearance of the closing identifier.

blog comments powered by Disqus