Jump to content

HEREDOC help


Scooby08

Recommended Posts

Could somebody let me know if this is the correct way to do this? It seems like it's not right because the closing javascript tag seems to escape the <<<EOF..

 

<?php
$test = "value";

return <<<EOF
<script language="JavaScript" type="text/JavaScript">
<!--
some javascript
-->
</script>
<form>
<input type="text" value="$test" />
</form>
EOF;
}
?>

 

Would I want the input line to be like it is, or like this:

 

<input type="text" value="<?=$test;?>" />

 

Link to comment
https://forums.phpfreaks.com/topic/145504-heredoc-help/
Share on other sites

In regards to your edit:

 

You don't need to do <?php ?> tags, because heredoc is a type of string and will parse variables automatically. See the example on the php.net page:

 

<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

/* More complex example, with variables. */
class foo
{
    var $foo;
    var $bar;

    function foo()
    {
        $this->foo = 'Foo';
        $this->bar = array('Bar1', 'Bar2', 'Bar3');
    }
}

$foo = new foo();
$name = 'MyName';

echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
?>

 

Will output:

My name is "MyName". I am printing some Foo.

Now, I am printing some Bar2.

This should print a capital 'A': A

Link to comment
https://forums.phpfreaks.com/topic/145504-heredoc-help/#findComment-763929
Share on other sites

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.