Page 1 of 1

I dont understan how to echo a mix of HTML and PHP

Posted: Thu Aug 25, 2011 9:26 am
by markosjal
I have the following in a site

Code: Select all

<img src="/buttons/<? echo 0; ?>/ad<? echo $linenum; ?>.png" alt="<?php echo $alttxt; ?>" border="0">

I want to be able to alternately load a GIF in place of a PNG using if@file_exists

something like

Code: Select all


<?php
if(@file_exists('/buttons/0/ad'.$linenum.'.png'))


   {
       echo "<img src='/buttons/0/ad'.$linenum.'.png' alt=".$alttxt " border="0">";
   }
else
   {
       echo "<img src='/buttons/0/ad'.$linenum.'.gif' alt=".$alttxt " border="0">";
  }
?>
I think I am not getting all the quotes in place and think I need to use "\" but have tried various combinations to no avail. I know the above example is wrong but hopefully it gets the idea across


Any help appreciated.

Thanks

Mark

Re: I dont understan how to echo a mix of HTML and PHP

Posted: Thu Aug 25, 2011 9:48 am
by Celauran
I think it's the mixing of single and double quotes that's causing you problems. Try this:

Code: Select all

<?php
if(@file_exists('/buttons/0/ad'.$linenum.'.png'))
{
    echo "<img src=\"/buttons/0/ad{$linenum}.png\" alt=\"{$alttxt}\" border=\"0\">";
}
else
{
    echo "<img src=\"/buttons/0/ad{$linenum}.gif\" alt=\"{$alttxt}\" border=\"0\">";
}
?>

Re: I dont understan how to echo a mix of HTML and PHP

Posted: Fri Aug 26, 2011 2:11 am
by markosjal
Thanks this worked great but I needed a to start the path for if(@exists with "./" instead of just "/" although this was not necessary for the image file.

Mark

Re: I dont understan how to echo a mix of HTML and PHP

Posted: Fri Aug 26, 2011 8:29 am
by social_experiment
Divided wrote:although you can't embed variables with single quotes.
I think you can use single quotes for that purpose aswell

Code: Select all

<?php
 $value = 5;
 echo '<input type="text" name="field" value="' . $value . '" />';
 // should give the field a value of 5; 
?>