Author |
Message |
Satis
Felix Rex
Joined: Fri Mar 28, 2003 6:01 pm Posts: 16654 Location: On a slope
|
ternary operation
ok...this is cool but confused me for awhile. I get it now and it's not hard, but I'm going to share the syntax and a quick explanation for anyone that needs it (probably not gfree or pig, but I can use it as a reference and anyone that nubier than me may get something out of it.
Basically a ternary operation is shorthand for an if-then-else statement. php:
[php]
if($var == true){
$var = false;
}
else{
$var = true;
}
[/php]
that's a standard and very simple if/then/else statement in php. Any c-like language has something similar. the ternary equivalent is
[php]
$var = ($var == true) ? false : true;
[/php]
much, much, much easier to write, and once you're used to it, much easier to read a series of conditionals. Anyway, as a quick description
$var_to_set = (conditional statement) ? value_to_set_var_to_if_true : value_to_set_var_to_if_false;
easy enough, right?
here's a wikipedia entry on the subject
http://en.wikipedia.org/wiki/Ternary_operation
_________________ They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.
Last edited by Satis on Thu Feb 08, 2007 3:21 pm, edited 1 time in total.
|
Thu Feb 08, 2007 10:33 am |
|
|
Pig
Duke
Joined: Mon Mar 31, 2003 8:59 am Posts: 1358 Location: right behind you
|
I love ternaries. Once you get used to them, you use them constantly. It's so much easier and significantly less code.
|
Thu Feb 08, 2007 1:57 pm |
|
|
RB
Emperor
Joined: Wed Apr 16, 2003 1:25 am Posts: 2560
|
Re: ternary operation
I'm little off the context...
sample:
[php]
$var = ($var == true) ? false : true;
[/php]
replacement:
[php]
$var = !$var;
[/php]
_________________ ++
|
Thu Feb 08, 2007 5:13 pm |
|
|
Satis
Felix Rex
Joined: Fri Mar 28, 2003 6:01 pm Posts: 16654 Location: On a slope
|
heh, indeed. I was only trying to be simple. How about something a little more down to earth?
javascript:
[php]function imageEdit(){
var thespan = document.getElementById("imageUpload");
thespan.style.display = (thespan.style.display != 'none') ? 'none' : 'inline';
}[/php]
_________________ They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.
|
Fri Feb 09, 2007 7:36 am |
|
|
Pig
Duke
Joined: Mon Mar 31, 2003 8:59 am Posts: 1358 Location: right behind you
|
I use it a lot for overiding defaults.
[php]
$limit = ($_GET['limit']) ? $_GET['limit'] : PAGINATION_LIMIT;
[/php]
|
Fri Feb 09, 2007 12:00 pm |
|
|
RB
Emperor
Joined: Wed Apr 16, 2003 1:25 am Posts: 2560
|
(Since Sat posted JS...)
Oh, but they are widely used in OCC too.
Note the row of "?"-s. They belong to the ternary operators.
_________________ ++
|
Fri Feb 09, 2007 12:31 pm |
|
|
Satis
Felix Rex
Joined: Fri Mar 28, 2003 6:01 pm Posts: 16654 Location: On a slope
|
that, sir, is gobbledy-gook masquerading as code.
I actually looked over the shoulder of one of our programmers yesterday... he was trying to debug some code. It was all in c++ and it looked something like that, with comments dead center in the code block. As much as I know, sometimes I feel like I know not a goddamned thing.
_________________ They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.
|
Sat Feb 10, 2007 10:47 am |
|
|
RB
Emperor
Joined: Wed Apr 16, 2003 1:25 am Posts: 2560
|
Offtopic: why certain number of you keeps addressing me as "sir"?
Should I change my nick?
_________________ ++
|
Sat Feb 10, 2007 11:16 am |
|
|
Satis
Felix Rex
Joined: Fri Mar 28, 2003 6:01 pm Posts: 16654 Location: On a slope
|
nah, just a form of speech. Changing your nick won't matter.
A gfree by any other name is just as odd.
_________________ They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.
|
Sun Feb 11, 2007 1:39 am |
|
|
RB
Emperor
Joined: Wed Apr 16, 2003 1:25 am Posts: 2560
|
However, I dislike that 'sir'. Sounds kinda - distancing?
_________________ ++
|
Sun Feb 11, 2007 5:36 am |
|
|