Forum Moderators: coopster

Message Too Old, No Replies

What's wrong with my if/else shorthand?

Parse error, unexpected ','

         

neophyte

11:45 am on Mar 12, 2008 (gmt 0)

10+ Year Member



Hello All -

I always use the if/else shorthand syntax when I can, but I've got this one line that just won't leave me alone and I can't figure out where the offending ',' is.

Here's the snippet that throws the :

echo ($i < $crumbCount)
? '<li><a class="' , $class , '" href="' , $v , '">' , $k , '</a></li>', $nl
: '<li class="' , $class , '">' , $k , '</li>', $nl;

*****

When I write it out "long-hand" like the below, no errors...

if($i < $crumbCount)
{
echo '<li><a class="' , $class , '" href="' , $v , '">' , $k , '</a></li>', $nl;
}
else
{
echo '<li class="' , $class , '">' , $k , '</li>', $nl;
}

Where am I going wrong? I can't find it.

Neophyte

turbosaab

12:25 pm on Mar 12, 2008 (gmt 0)

10+ Year Member



Try using dots instead of commas. I think the commas are causing your string components to be interpreted as multiple parameters.

vincevincevince

1:23 pm on Mar 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Enclose each part of the shorthand-if with () when you are using multiple concacted components:

echo ($i < $crumbCount)
? ('<li><a class="' , $class , '" href="' , $v , '">' , $k , '</a></li>', $nl)
: ('<li class="' , $class , '">' , $k , '</li>', $nl);

neophyte

12:52 am on Mar 13, 2008 (gmt 0)

10+ Year Member



Turbosaab -

replacing the commas with dots DOES work... thank you very much.

Vincevincevince -

I tried you're solution as well, but - irritatingly enough - still no dice. Strange huh? When echoing multiple components to screen I've always used commas without issue (and many people say comma echoing is faster than 'dot' echoing but I've never benchtested the two.

So, I guess for short-hand if/else representations I'll be sticking to dots.

Thanks to you both!

Neophyte

turbosaab

4:44 am on Mar 13, 2008 (gmt 0)

10+ Year Member



Excellent! I love it when I can actually get this stuff to work :-)