PHP – echo inside an echo

I have a PHP if/else statement. This is the code I’m trying to echo under an else condition.

<?php $locked = ForumData::is_topic_locked($post->topic_id);
            if ($locked->topic_locked == 1) {echo '<td align="right"><font color="#FF0000">Topic Locked</font><td>';}
            else { 
            echo '<td align="left"><a href="'.url('Forum/create_new_post?topic_id='.$post->topic_id.'&forum_id='.$post->forum_id.'').'" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"><img src="<?php echo SITE_URL?>/lib/skins/flyeuro/images/forums/t_reply.gif"/></a></td>'; }
            ?>

The bit I’m interested to echo is this.

<img src="<?php echo SITE_URL?>

If I try this… ‘echo SITE_URL’

Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';'

But this doesn’t parse the image, and if I try parsing anything else, it’s giving me parsing errors, which I can’t fix?

How can I therefore produce an echo inside another echo?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

why did you open a <?php tag again, you are already in echo line?

echo '<td align="left"><a href="'.url('Forum/create_new_post?topic_id='.$post->topic_id.'&forum_id='.$post->forum_id.'').'" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"><img src="'.SITE_URL.'/lib/skins/flyeuro/images/forums/t_reply.gif"/></a></td>';

and what is SITE_URL? Is that a variable, did you forget to put $?


echo prints out the string that you gave as parameter,

echo "foo";

As @hakre mentioned about it, . is used to concatenate strings.

$var = "foo"."bar";  //foobar

So you can use it in echo line,

$var = "foo"."bar";  //foobar
echo "foo "."bar ".$var // foo bar foobar

And It’s not important weather variable defined as a string. It would be a constant variable.

define('SITE_URL', 'localhost:8080/phpvms'); 
echo "my website URL is ".SITE_URL; //my website URL is localhost:8080/phpvms

Solution 2

Remember:

<?php echo "<a href=\"$url\">View</a>"; ?>

” and \

this!

Hope that’s enough of a [email protected]

Solution 3

Your problem is probably solved this way:

echo '<td align="left"><a href="',
     url('Forum/create_new_post?topic_id=' . $post->topic_id . '&forum_id=' . $post->forum_id . '') ,
     '"><img src="', SITE_URL,
              #######################
     '/lib/skins/flyeuro/images/forums/t_reply.gif"/></a></td>';

In PHP you can use constants quite like variables, e.g. to output them. You don’t need to stack echoes inside each other or something.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply