Forum Moderators: coopster

Message Too Old, No Replies

If shorthand notation without the "else"

What's the syntax? Does it exist?

         

neophyte

12:51 am on Jun 19, 2006 (gmt 0)

10+ Year Member



Hello all -

I'm writing a script to select the current month within a select list.

What I've got so far - that works - is the following:

<select>
<option value="January" <?php echo ($month == '01'? "selected" : "");?>>January</option>
... (and so on)
</select>

Works beatifully. However, I've always wanted to shorten the syntax to get rid of the else part, like: <?php echo ($month == '01'? "selected");?> but it won't let me do it - throws parse errors.

I've looked for documentation on a shorter-shorthand conditional statement, but I can't find one.

Is it impossible to use this shorthand statement without including the "else" part?

Neophyte

jatar_k

1:40 am on Jun 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you're using the ternary operator then you always need both

you could use a straight if but I don't know if it is shorter

<?php if($month=='01') echo 'selected';?>
as opposed to
<?php echo ($month == '01'? "selected" : "");?>

though if you want even shorter then you could loop it. take a look at
Dynamic Date Selector [webmasterworld.com] msg 5 for what I mean

neophyte

2:17 am on Jun 19, 2006 (gmt 0)

10+ Year Member



jatar_k -

The straight if statement is slightly shorter, AND much more understandable. Thanks.

The dynamic date selector is TOO COOL.

Neophyte