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?

hakre
  • 193,403
  • 52
  • 435
  • 836
zzwyb89
  • 472
  • 1
  • 7
  • 21
  • 1
    You have `}` at the end - where does it belong to? And the if-else statement is missing. – hakre Jan 05 '13 at 23:32
  • The } closes the else statement. I'll edit my main post for clarity. – zzwyb89 Jan 05 '13 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: http://3v4l.org/b4NtM – hakre Jan 05 '13 at 23:45

3 Answers3

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
Community
  • 1
  • 1
Okan Kocyigit
  • 13,203
  • 18
  • 70
  • 129
  • 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', 'http://localhost:8080/phpvms'); – zzwyb89 Jan 05 '13 at 23:48
  • A dot in PHP is used to concatenate two strings. http://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: http://stackoverflow.com/a/14177712/367456 - a bit special to `echo`. – hakre Jan 05 '13 at 23:49
  • 1
    Thank you for the full explanation, just got an example sorted and working to enhance my humble PHP knowledge :) – zzwyb89 Jan 06 '13 at 00:06
1

Remember:

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

" and \

this!

Hope that's enough of a hint!@

Head
  • 548
  • 7
  • 26
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.

hakre
  • 193,403
  • 52
  • 435
  • 836