Forum Moderators: coopster

Message Too Old, No Replies

echo problem

echo an echo

         

shnoop

9:30 pm on Oct 15, 2004 (gmt 0)

10+ Year Member



Hello guys, I have a newbie question... I can't put that into proper syntax...


<? if($picnr==1){
echo("");
}
else {
echo "<a href=\""<? echo ($urlprev)?> "\">previous picture </a>"";
}
?>

I hope you can help me, thank you very much...

shnoop

yowza

9:36 pm on Oct 15, 2004 (gmt 0)

10+ Year Member



First of all, why are you echoing nothing if the value equals 1?

Instead do this:

<?
if($picnr!= 1){
echo "<a href=\"".$urlprev."\">previous picture </a>";
}
?>

shnoop

9:58 pm on Oct 15, 2004 (gmt 0)

10+ Year Member



Because if the first picture is shown there is no previous picture... Thx for the answer anyway, i think i can do it now (otherwise i ask again...)

jatar_k

10:55 pm on Oct 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



how about a few tips

it can be written like so

if($picnr==1){
echo("");
} else {
echo "<a href=\"".$urlprev."\">previous picture </a>"";
}

with echo you can put a comma between the different elements instead of concatenating them together, this is actually faster than having to cat it first and then echo it. So like so

if($picnr==1){
echo("");
} else {
echo "<a href=\"",$urlprev,"\">previous picture </a>"";
}

if statements that are followed with only a single line do not need braces therefore this way

if($picnr==1) echo("");
else echo "<a href=\"",$urlprev,"\">previous picture </a>"";

as yowza mentioned, if you need it to do nothing when $picnr==1 then do the inverse and leave out the else all together, if the test is false nothing will happen

if($picnr!= 1) echo "<a href=\"",$urlprev,"\">previous picture </a>"";

seems the shortest way ;)

<added>I try to always have the most common case appear first. So if you wanted to actually echo something when $picnr == 1 do it this way

if($picnr!= 1) echo "<a href=\"",$urlprev,"\">previous picture </a>"";
else echo '&nbsp;';