PHP里面的PHP内部[重复]

This question already has an answer here:

I can't make the following work. I know it's because of the PHP function inside the string of HTML, but I don't know how to fix it.

echo '<aside class="tipContainer">'.'<div>'.'<h1>'.$header.'</h1>'.'<img src="<?php bloginfo('template_url'); ?>/images/pencil_Tip.gif" alt="">'.'</div>'.'<p>'.$content.'</p>'.'</aside>';

I receive this error: unexpected T_STRING, expecting ',' or ';' in ....

</div>

PHP is nested too deep. (php nested in php). Try this:

echo '<aside class="tipContainer"><div><h1>'.$header.'</h1><img src="'.bloginfo("template_url").'/images/pencil_Tip.gif" alt=""></div><p>'.$content.'</p></aside>';

Try:

echo '<aside class="tipContainer"><div><h1>' . $header . '</h1>'.
     '<img src="' . get_bloginfo('template_url') . '/images/pencil_Tip.gif" ' .
     'alt=""></div><p>' . $content . '</p></aside>';

Since you are already inside <?php ?> tags for your echo statement, you don't need them when you want to call get_bloginfo(). Just call the function as part of the string concatenation.

echo '<aside class="tipContainer">'.'<div>'.'<h1>'.$header.'</h1>'.'<img src="' . bloginfo('template_url') . '/images/pencil_Tip.gif" alt="">'.'</div>'.'<p>'.$content.'</p>'.'</aside>';

Try this:

echo '<aside class="tipContainer"><div><h1>' . $header . '</h1><img src="'
. bloginfo('template_url')
. '/images/pencil_Tip.gif" alt=""></div><p>' . $content . '</p></aside>';

Explanation why it not works:

'<img src="<?php bloginfo('template_url'); ?>/images ...
'______ php string _______'            '__ another php string ...
                           ^^^^^^^^^^^^
                           this is not part of the sting,
                           because you just closed it.

Solutions:

  • 'aaa' . "'bbb'" . 'ccc' : alternate quoting style
  • 'aaa\'bbb\'ccc' : quote the single-quote in the string
  • "aaa'bbb'ccc" : use double quotes to quote a string containing single quotes

Try:

//stop executing PHP, go to plain HTML
 ?>
<aside class="tipContainer">
<div>  
    <h1><?php echo $header?> </h1>
    <img src="<?php bloginfo('template_url'); ?>/images/pencil_Tip.gif" alt="">
</div>
<p>
<?php echo $content ?>
</p>
</aside>

<?php //start php again

often makes for cleaner, easier to maintain code to interweave php into html, rather than the other way aroudn

echo '<aside class="tipContainer"><div><h1>'.$header.'</h1><img src="' . bloginfo('template_url') . '/images/pencil_Tip.gif" alt=""></div><p>'.$content.'</p></aside>';

try the below,

--> Avoid as much concatenation as possible, no need to split/concatenate strings often, ex : "string1" . "string2" is not required, simply do "string1string2" to avoid confusion.

--> Use an IDE, It helps a lot to debug syntax errors.

<?php
echo '<aside class="tipContainer"><div><h1>'. $header. '</h1><img src="' . bloginfo('template_url') . '"/images/pencil_Tip.gif" alt=""></div><p>'.$content.'</p></aside>';
?>