摆脱heredoc的空白

时间:2012-09-16 13:20:54

标签: php

我有这个循环

foreach ($unserialized as $key => $value) {
 $foo = <<<EOT
<div class='krudItem' id='xxx'><form class='aj' name='itemForm' method='post'
 action=''><section><label>Slider Title</label><input type='hidden'
 name='sliderKey' value=$key/><input type='text' name='sliderTitle' value='$value 
 ['slidertitle']'/></section><section> <label>Slider Location</label>
 <input type='text' 
 name=$value['sliderlocation']value='ipsum'/></section><section><label>Slider 
 Description</label><textarea name='sliderDescription'>$value
 ['sliderdescription']</textarea></section><button name='saveNew' class='saveNew' 
 value='save'>save</button><button name='newCancel' value='cancel' 
 class='deleteNew'>cancel</button></form></div>
EOT;
echo $foo;

但每次我运行它,我都会

  

解析错误:语法错误,意外''(T_ENCAPSED_AND_WHITESPACE),

我一直在阅读php关于http://us2.php.net/tokens的内容,我已经尝试了所有其他可能的解决方案,但错误仍然存​​在。有一些空格,但我不确定我将如何处理它们。

2 个答案:

答案 0 :(得分:3)

您需要将变量括在花括号中:

$str = <<<EOF
some string with {$some['variables']} in it
EOF;

您可以将任何PHP表达式放在{...}中,只要它以$开头。

请参阅the documentation on this syntax at php.net

答案 1 :(得分:2)

您需要将变量括在括号中。

这应该适合你:

<?php
foreach ($unserialized as $key => $value) {
    $foo = <<<EOT
    <div class='krudItem' id='xxx'>
        <form class='aj' name='itemForm' method='post' action=''>
            <section>
                <label>Slider Title</label>
                <input type='hidden' name='sliderKey' value={$key}/>
                <input type='text' name='sliderTitle' value='{$value['slidertitle']}'/>
            </section>
            <section>
                <label>Slider Location</label>
                <input type='text' name={$value['sliderlocation']} value='ipsum'/>
            </section>
            <section>
                <label>Slider Description</label>
                <textarea name='sliderDescription'>
                    {$value['sliderdescription']}
                </textarea>
            </section>
            <button name='saveNew' class='saveNew' value='save'>save</button>
            <button name='newCancel' value='cancel' class='deleteNew'>cancel</button>
        </form>
    </div>
EOT;
    echo $foo;
}