How to put HTML in PHP code

I currently have the following html

[code]



<?=$caption;?>

[/code]

and would like to be able to select one of two values using an PHP IF statment but I cannot get the code right. Can you please help? This is what I have:

[php]<?php
if ($next < $last) {
echo ’ ‘;
echo ‘

’.$caption.’

’;
}
else {
echo ’ ‘;
echo ‘

’.$caption.’

’;
}
?>[/php]

Thanks Freddo

When you echo variables in php, they cannot be surrounded by apostrophes. You can use double quotes, or no quotes at all (when you aren’t outputting additional text). See if the following works for you:
[php] if ($next < $last)
{
echo “”;
echo “

$caption

”;
}
else
{
echo “”;
echo “

$caption

”;
} [/php]

I am assuming WEBIMGDIR is a previously defined constant.

If this doesn’t work, let me know and we will figure it out.

Sorry that I probably should have been clearer regarding using apostrophes (single-quotes).

If the echo statement starts and ends with double-quotes ("…"), the value of variables inside the double-quotes will be substituted in place of the variables upon output.

If the echo statement starts and ends with single-quotes (’…’), variables inside the single quotes will be treated as literal text instead of variables and their values will not be substituted.

In the example I presented, the echo statements use double quotes (with single quotes inside them), so the variable values will be substituted on output.

Thanks for your reply malasho and the explanation of the quotes.
I have tried the code you posted but it does not output the resulting image.

Yes WEBIMGDIR variable is define as:
[php]define (‘WEBIMGDIR’, ‘/images/slideshow/’); [/php]
and $curr and $last are the names of image files.

Can you help further please?

I apologize, the WEBIMGDIR constant was not handled the way I expected it to be in the code I provided before.

Try this:

[php]
if ($next < $last)
{
$link = WEBIMGDIR.$curr;
echo “”;
echo “

$caption

”;
}
else
{
$link = WEBIMGDIR.$last;
echo “”;
echo “

$caption

”;
}
[/php]

You could also do the following depending on your preference (I like the option above better myself):
[php] if ($next < $last)
{
echo “”;
echo “

$caption

”;
}
else
{
echo “”;
echo “

$caption

”;
}[/php]

Sorry again for the previous error! I have tested both of these and they seem to be producing the expected results.

Thanks malasho - I appreciate your help.
I managed to get it to work correctly by setting the values to a variable just like in you first option in your last post.
Thanks for your help.

You’re welcome, glad you got it working!

Sponsor our Newsletter | Privacy Policy | Terms of Service