Jump to content

Special Characters Problem


dapcigar

Recommended Posts

Be aware that heredocs are interpreted like double-quoted strings, so character sequences like $x or \n will be interpreted in a special way. If you want plain text like in a single-quoted string, use a nowdoc:

<?php

$str = <<<'MYTEXT'
Example of string
spanning multiple lines
using nowdoc syntax.
MYTEXT;

This makes absolutely no sense.

 

The part after the “<<<” is the delimiter for the heredoc/nowdoc block. It simply tells PHP where the block begins and where it ends. You do not use variables or anything like that. Just make up an identifier:

<?php

$comment = $_POST['comment'];

$str = <<<'I_AM_THE_DELIMITER'
Example of string
spanning multiple lines
using nowdoc syntax.
I_AM_THE_DELIMITER;

If you want to insert some variable, you have to insert it into the block between the delimiters:

<?php

$comment = $_POST['comment'];

$str = <<<I_AM_THE_DELIMITER
This is the user comment: $comment
I_AM_THE_DELIMITER;

Note that you need a heredoc if you want to directly insert variables like in a double-quoted string. A nowdoc doesn't do that (as I already explained).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.