kusanagi
kusanagi

Reputation: 14614

php formatting string EOD

i got the code

$confirm = "if (!confirm('".Module::t("Approve") . "?')) return false;";

$js_approve =<<< EOD
function() {
    $confirm
    var url = $(this).attr('href');
    $.post(url, function(response) {
        alert(response);
    });
    return false;
}
EOD;

is it possible to transform to code like this

    $js_approve =<<< EOD
    function() {
        "if (!confirm('".Module::t("Approve") . "?')) return false;";
        var url = $(this).attr('href');
        $.post(url, function(response) {
            alert(response);
        });
        return false;
    }
    EOD;

any ideas?

Upvotes: 1

Views: 1264

Answers (4)

Toto
Toto

Reputation: 91373

Apart that you can't indent, you can't also write this line

"if (!confirm('".Module::t("Approve") . "?')) return false;";

inside the heredoc.

Upvotes: 2

Bojangles
Bojangles

Reputation: 101473

From this bit in the PHP docs:

It is very important to note that the line with the closing identifier must contain no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter (possibly followed by a semicolon) must also be followed by a newline.

It says that the identifier may not be indented. I like to keep my indentation clean, and you can carry on as you would normally, but the only thing you must pay attention to is that the closing EOD; has no other characters before it on that line (ie, it must have a preceding newline). Other than that, everything will work the way you expect it to.

Taking your code as an example:

    $js_approve =<<< EOD
    function() {
        "if (!confirm('".Module::t("Approve") . "?')) return false;";
        var url = $(this).attr('href');
        $.post(url, function(response) {
            alert(response);
        });
        return false;
    }
EOD;

Would work fine

Upvotes: 0

sitios
sitios

Reputation: 1

To prevent PHP from parsing the text use nowdoc syntax (since PHP 5.3.0)

Upvotes: 0

Michiel Pater
Michiel Pater

Reputation: 23023

No, it is not. You may not use indenting for heredoc strings.

You should read the warning on this page: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Upvotes: 2

Related Questions