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;
Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.