Penguin
Blame: TernaryOperator
EditPageHistoryDiffInfoLikePages
Annotated edit history of TernaryOperator version 4, including all changes. View license author blame.
Rev Author # Line
3 SamCannell 1 The "?:", or Ternary operator in PHP, Java, Perl, C and possibly other languages is essentially shorthand for a block of code like:
1 SamCannell 2
3 if ($a==1) {
4 print "A is 1!";
5 } else {
6 print "A is not 1!";
7 }
8
9 Using the ternary operator, it becomes:
10
2 SamCannell 11 print ($a==1) ? "A is 1!" : "A is not 1!";
1 SamCannell 12
13 Cool, huh?
14
15 Or, in java:
16
17 public class !CondOpTest {
18 public static void main(String[] args) {
19 new !CondOpTest(new Integer(args[[0]));
20 }
21
22 public !CondOpTest(Integer a) {
23 if (a.intValue()==1) {
24 System.out.println("A is 1!");
25 } else {
26 System.out.println("A is not 1!");
27 }
28 }
29 }
30
31 becomes:
32
33 public class !CondOpTest {
34 public static void main(String[] args) {
35 new !CondOpTest(new Integer(args[[0]));
36 }
37
38 public !CondOpTest(Integer a) {
2 SamCannell 39 System.out.println((a.intValue()==1 ) ? "A is 1!" : "A is not 1!");
1 SamCannell 40 }
41 }
42
2 SamCannell 43 In Perl, however, things are slightly different.
44
45 This piece of code is valid, and is similar to the examples above:
46
47 print ( ($a==1) ? "A is 1!" : "A is not 1!" );
48
49 However the following code is also valid:
50
51 ($a==1) ? print "A is 1!" : print "A is not 1!";
4 DanielLawson 52
53
54 The above also works in C
55
56 int a = 0;
57 (a == 1) ? printf("A is 1!\n") : printf("A is not 1!\n");
The following authors of this page have not agreed to the WlugWikiLicense. As such copyright to all content on this page is retained by the original authors. The following authors of this page have agreed to the WlugWikiLicense.

PHP Warning

lib/plugin/WlugLicense.php (In template 'html'):99: Warning: Invalid argument supplied for foreach()

lib/plugin/WlugLicense.php (In template 'html'):111: Warning: in_array() [<a href='function.in-array'>function.in-array</a>]: Wrong datatype for second argument

lib/plugin/WlugLicense.php (In template 'html'):111: Warning: in_array() [<a href='function.in-array'>function.in-array</a>]: Wrong datatype for second argument