2

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.'').'"><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?

3
  • 1
    You have } at the end - where does it belong to? And the if-else statement is missing. Commented Jan 5, 2013 at 23:32
  • The } closes the else statement. I'll edit my main post for clarity. Commented Jan 5, 2013 at 23:33
  • I have problems to follow your quesiton, I can not reproduce that parse error. Probably that is to some other code. See here your code in many PHP versions: 3v4l.org/b4NtM Commented Jan 5, 2013 at 23:45

3 Answers 3

3

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.'').'"><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
Sign up to request clarification or add additional context in comments.

3 Comments

Could you explain to me for future reference what the dots do in PHP if you don't mind? The SITE_URL is a string of the website address gained from a config.php file in a CMS. It is setup during the CMS installation. i.e. define('SITE_URL', 'localhost:8080/phpvms');
A dot in PHP is used to concatenate two strings. php.net/manual/en/language.operators.string.php - that define defines a constant (as a string), so you can use the dots here to contanate strings with strings. In my answer I just use echo with multiple string expressions separated by a comma, that is similar but also different: stackoverflow.com/a/14177712/367456 - a bit special to echo.
Thank you for the full explanation, just got an example sorted and working to enhance my humble PHP knowledge :)
1

Remember:

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

" and \

this!

Hope that's enough of a hint!@

4 Comments

Oh, is it that easy? Correct me if I'm wrong <img src="<?php echo \"SITE_URL\"?> ... ;>
Might be that you need the double quotes around the html.
maybe put the whole thing in an include file. might work as well. just adding my two cents.
Stackoverflow is not for "hints", explain your answer like the rest of us or don't bother posting
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.