Heredoc in PHP returns Error: Undefined variable: using sprintf

2

I define the content of a variable in PHP using HEREDOC

$ptrn = <<<EOF
En %2$s se ha bebido %1$d cervezas!
EOF;

When I try to format with sprinf

$ptrn = sprintf($ptrn,7,"Paco");

returns the following error

  

Notice: Undefined variable: s in   C: \ xampp \ htdocs \ androidtemplates \ test.php on line 7

    
asked by Webserveis 17.08.2016 в 12:34
source

1 answer

2

When you use <<<EOF .. EOF; ( Heredoc syntax ) Variables are interpreted - in the same way as if they were commas ( " ).

To avoid this you can use the syntax nowdoc that acts as single quotes ( ' ).

$ptrn = <<<'EOF'
En %2$s se ha bebido %1$d cervezas!
EOF;
    
answered by 17.08.2016 / 12:43
source