1

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 ....

2
  • echo '<aside class="tipContainer">'.'<div>'.'<h1>'.$header.'</h1>'.'<img src="'.bloginfo('template_url').'/images/pencil_Tip.gif" alt="">'.'</div>'.'<p>'.$content.'</p>'.'</aside>'; Commented Jun 27, 2012 at 17:09
  • 2
    You don't need to concatenate html tags with. (e.g: ) '<div>'.'<h1>' could be changed to '<div><h1>'. Commented Jun 27, 2012 at 17:12

8 Answers 8

11

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thats just ugly. look at Marvos answer.
I edited it and it's under review :-)
I'll upvote if you remove the unneeded concatenations.
@Thomas I voted to approve the edit, 1 more needed. Also cleaned up my answer. thanks.
5

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>';

6 Comments

You can remove the concatenation being done here '.'<div>'.'
Ok, thanks this did return the URL I need but it did not place it inside the img element's src attr. The URL now appears as a string outside the aside element.
good call @j08691 . It was some rather deeply nested code, and I had to make sure not to use single quotes when they meant to output strings when it was within another string (it's like inception :) ) and I overlooked this.
@MarkBubel this is because WordPress getbloginfo echo's the output. Use get_bloginfo() instead.
Aha... thanks, William! It now works as expected.
|
4

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

Comments

2

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>';

6 Comments

+1 removed unneeded concatenation
Okay, cool. But why did you ADD unnecessary concatenation by pulling $header and $content out of the string? You don't need to do that for simple variables like that in PHP.
That doesn't work. Variables inside single quotes are not interpreted
Yep, you're right. I must use double quotes instinctively, and my test of this had double quotes. Learn something new daily.
Also note that there were much discussions on what is faster, single or double quotes. But today it doesn't really matter. There are operations (database, regular expressions, ...) which need a magnitude more time.
|
0
echo '<aside class="tipContainer">'.'<div>'.'<h1>'.$header.'</h1>'.'<img src="' . bloginfo('template_url') . '/images/pencil_Tip.gif" alt="">'.'</div>'.'<p>'.$content.'</p>'.'</aside>';

Comments

0

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

Comments

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

Comments

0

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>';
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.