PHP Heredoc String Rules

Can anyone here learn the intricacies of using heredoc in PHP with PHP code examples? Based on what the guide says below?

The php.net manual says:

It is very important to note that a line with a closing identifier should not contain other characters, except, possibly, a semicolon ( ;). This means that the identifier cannot be indented, and there should be no spaces or tabs before or after the semicolon. It is also important to understand that the first character before the closing identifier must be a new line defined by the local operating system. This is \non UNIX systems, including Mac OS X. The final delimiter (possibly followed by a semicolon) should also follow a new line.

If this rule is violated and the closing identifier is not "clean", it will not be considered as a closing identifier, and PHP will continue to search for it. If the correct close identifier is not found before the end of the current file, a parsing error will be displayed on the last line.

Heredocs cannot be used to initialize class properties. Starting with PHP 5.3, this restriction is only valid for heredocs containing variables.

Here is a link to the php manual: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

+5
source share
4 answers

this is a simple use:

$bar = "stackoverflowpro";
$foo = <<<HTML
<p>Hello $bar</p>
HTML;
+8
source
echo <<< _html

... some html code here ...

_html;

, , "< lt; < < ( , ) ";".

: "_" , , heredoc.

+6

heredoc , ; IMMEDIATELY . , heredoc. , , ;. ; ; . , heredoc , ( , ) ..).

, , , (;) . chararcter ( End-Of-File) , PHP.

heredoc:

$text = <<<EOT
Hello!
EOT;

heredoc ,   EOT; :

  $text = <<<EOT
  Hello!
  EOT;

:

  $text = <<<EOT
  Hello!
EOT;

( , ); ):

print(<<<EOT
Hello!
EOT
);

, , ( , , - ). Hello! :

           print(<<<EOT
     Hello!
EOT
                     );
+5
source

Here is an example of an example that covers all the possible uses of both heredoc and nowdoc:

<?Php
if (isset($_GET['css']))
{   // SET THE CONTENT TYPE
    header("Content-type: text/css");
    echo // ECHO THE CSS STRING USING HEREDOC
    // AS YOU NOTICE HERE, WE INSERT THE GET DATA WITH "{}" TO MAKE IT EASIER TO DISTINGUISH THE VARIABLES
<<<MYCSS
button#{$_GET['css']}
{     color:white;
      background-color:black;
      padding:10px;
}
MYCSS;
    // IMMEDIATELY EXIT THE PHP  TO SKIP THE REST OF THE CODE
    exit();
}
else if (isset($_GET['js']))
{   // SET THE CONTENT TYPE
    header("Content-type: text/javascript");
    echo // ECHO THE JAVASCRIPT STRING USING HEREDOC
    // AS YOU NOTICE HERE, WE INSERT THE GET DATA WITH "{}" TO MAKE IT EASIER TO DISTINGUISH THE VARIABLES
<<<MYJS
function doSomething(param)
{   alert(param);
}
$(document).ready
(   function()
    {   $("button#{$_GET['js']}").click
        (   function()
            {   doSomething('do me');
            }
        );
    }
);
MYJS;
    // IMMEDIATELY EXIT THE PHP  TO SKIP THE REST OF THE CODE
    exit();
}

$myAjax = 'https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js';
$buttonID = md5(time().rand(111111,999999));
$myBottom = "\t".'\tlower body';
$doMe= "give head";
$head = <<<GIVEAHEAD
<html>
<head>
    <title>An advanced use of heredoc and more</title>
    <script type='text/javascript' src="$myAjax"></script>
    <script type='text/javascript' src="{$_SERVER['PHP_SELF']}?js={$buttonID}"></script>
    <link rel="stylesheet" href="{$_SERVER['PHP_SELF']}?css={$buttonID}" type='text/css'>
</head>
GIVEAHEAD;

// HERE A VARIETY OF WAYS YOU CAN INSERT A HEREDOC INSIDE A FUNCTION
echo iScream
(     $doMe,
      $head
,<<<SHOWMYBODY

<body>
SHOWMYBODY
. 
<<<_TATSMAHBAREH

    <button id='$buttonID'>touch me
_TATSMAHBAREH
// YOU CAN BASICALLY PUT YOUR '.' ANYWHERE BEFORE A HEREDOC, BUT NOT AFTER A HEREDOC
.pushIt().
<<<"FINISHOFF"

{$myBottom}
\taye\n
FINISHOFF
// A NOW DOC IS THE EQUIVALENT OF SINGLE QUOTE STRINGS
// IT DOES NOT RECOGNIZE VARIABLES OR \t AND \n
,<<<'ThisIsANowDoc'
    $ThisWillNotBeTreatedAsAVariable
    \tnope
</body>
</html>
ThisIsANowDoc
// A HEREDOC OR A NOWDOC CAN BE FOLLOWED BY A SEMICOLON (;), BUT NOT WITH ANYTHING ELSE
);

// RETURNING A HEREDOC FROM A FUNCTION
function pushIt()
{   return <<<YOLO
</button>
YOLO;
// ENDING A HEREDOC; YOU CANNOT PUT ANYTHING ELSE
// AFTER THE SEMICOLON - NOT EVEN A WHITESPACE
}

function iScream($act,$insert,$here,$below)
{   if($act == 'give head')
    return "$insert$here$below";
}
?>
+1
source

All Articles