Forum Moderators: coopster

Message Too Old, No Replies

Php Simple Ternary Question

         

drooh

10:11 pm on Nov 9, 2009 (gmt 0)

10+ Year Member



Is there a way to omit the else part of a ternary statement if its null?

For instance Can I do something like this?

Original
<?=($recurring_event_mon)?" checked=\"checked\"":null?>

Omit end
<?=($recurring_event_mon)?" checked=\"checked\"";?>

eelixduppy

10:32 pm on Nov 9, 2009 (gmt 0)



Pretty sure that is not valid syntax. Why are you trying to remove the end there? I mean, it's already shortened syntax to begin with. ;)

drooh

11:48 pm on Nov 9, 2009 (gmt 0)

10+ Year Member



cute

but if your gonna code for the rest of your life saving a few characters saves time

so is this possible?

rocknbil

12:55 am on Nov 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You don't need checked="checked", there's a savings right there.

<input type="checkbox" name="some-name" value="some-val" checked>

I don't know of a singular, but in the context of a checkbox, it is set or it is not, an unchecked value will not be in post/get/request. Use this to your advantage when setting $recurring_event_mon:

$recurring_event_mon=($_POST['some-val'])?' checked':NULL;

echo '<input type="checkbox" name="some-name" value="some-val"' . $recurring_event_mon . '>';

eelixduppy

2:05 am on Nov 10, 2009 (gmt 0)




but if your gonna code for the rest of your life saving a few characters saves time

The example you wrote saves you typing 4 characters...You'll find as you write more and more code that saving yourself typing is not the best approach to sometimes even the simplest -- or what may seem so at the time -- tasks. A MUCH better approach would be to write readable code and not worry so much about how much characters you are using. In the long run, writing readable code will save you much more time.

eelixduppy

2:05 am on Nov 10, 2009 (gmt 0)



>> so is this possible?

No.

coopster

2:52 pm on Nov 14, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Side note:

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

Just a heads up.

[php.net...]

bedlam

12:20 am on Nov 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You don't need checked="checked", there's a savings right there.

If you're coding in xhtml, you do need 'checked="checked"' [w3.org]...

-- b

rocknbil

6:34 pm on Nov 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes I know that, and is relevant, and I should have added an "if," sorry. :-) However most of the pages I see don't even need XHTML, the output is vanilla HTML with the exception of the added /> characters, their servers are outputting text/html instead of text/xhtml, and most people don't even know why they are choosing an XHTML doctype or the implications. Some don't even know they are choosing a doctype at all. I only mentioned it in the context of "saving characters."