1

我有这个循环

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的内容,并且我尝试了所有其他可能的解决方案,但错误仍然存​​在。有一些空格,但我不确定如何处理好它们。

4

2 回答 2

3

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

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

您可以放入任何 PHP 表达式,{...}只要它以$.

请参阅php.net 上有关此语法的文档

于 2012-09-16T13:24:46.127 回答
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;
}
于 2012-09-16T13:22:33.367 回答