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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

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

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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\">";
}
?>
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

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

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

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

Post 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; 
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply