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
markosjal
Forum Commoner
Posts: 63 Joined: Fri Apr 16, 2010 10:15 pm
Post
by markosjal » Thu Aug 25, 2011 9:26 am
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
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Thu Aug 25, 2011 9:48 am
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
Post
by markosjal » Fri Aug 26, 2011 2:11 am
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
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Fri Aug 26, 2011 8:29 am
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