Professional Web Applications Themes

Operator ternary - PHP Development

Look at the operator ternary: $flag = true; ($flag)?($a = "yes"):($a = "no"); Now i want to have multiple statements in my operator ternary something like (does not work!!!): ($flag)?($a = "yes";$b="yes"):($a = "no";$b="no"); One way to make this work is: ($flag)?($a = "yes" and $b="yes"):($a = "no" and $b="no"); but to me this is not logical. Does somebody have any ideas about this? regards, Marcel...

  1. #1

    Default Operator ternary

    Look at the operator ternary:

    $flag = true;

    ($flag)?($a = "yes"):($a = "no");

    Now i want to have multiple statements in my operator ternary something like
    (does not work!!!):

    ($flag)?($a = "yes";$b="yes"):($a = "no";$b="no");

    One way to make this work is:

    ($flag)?($a = "yes" and $b="yes"):($a = "no" and $b="no"); but to me this is
    not logical.

    Does somebody have any ideas about this?

    regards,

    Marcel


    Marcel Guest

  2. #2

    Default Re: Operator ternary

    Marcel said the following on 02/06/2005 10:21: 

    The ternary operator is not designed to execute code. It's designed for
    uses such as:

    $a = $flag : "yes" : "no";

    i.e. assigning a value dependent on the logical state of $flag. It just
    so happens that your code above works, because '$a = "yes"' evaluates to
    a value, and so can be used in the ternary operator.

    Why not just use if? That's what it's there for! And it's far more
    obvious what's going on than trying to cram a load of code onto one line.

    if ($flag)
    {
    ...
    }
    else
    {
    ...
    }


    --
    Oli
    Oli Guest

  3. #3

    Default Re: Operator ternary


    "Oli Filth" <co.uk> schreef in bericht
    news:bvAne.4425$ntli.net... 
    >
    > The ternary operator is not designed to execute code. It's designed for
    > uses such as:
    >
    > $a = $flag : "yes" : "no";
    >
    > i.e. assigning a value dependent on the logical state of $flag. It just so
    > happens that your code above works, because '$a = "yes"' evaluates to a
    > value, and so can be used in the ternary operator.
    >
    > Why not just use if? That's what it's there for! And it's far more obvious
    > what's going on than trying to cram a load of code onto one line.
    >
    > if ($flag)
    > {
    > ...
    > }
    > else
    > {
    > ...
    > }
    >
    >[/ref]

    Ok thanks that what i was thinking too!

    Regards,

    Marcel


    Marcel Guest

  4. #4

    Default Re: Operator ternary

    "Marcel" <com> wrote in message
    news:de175$429ecf8b$3e3a8507$versatel.nl... 


    You're supposed to use it like this:
    $a = $flag ? 'yes' : 'no';
    $b = $flag ? 'yes' : 'no';
    $c = $flag ? 'michael' : 'knight';
    etc.. Or like Oli said, use if statement...

    --
    Welcome to Usenet! Please leave tolerance, understanding
    and intelligence at the door. They aren't welcome here.
    eternal piste erection miuku gmail piste com


    Kimmo Guest

Similar Threads

  1. Ternary operator question
    By Perl in forum PERL Beginners
    Replies: 12
    Last Post: March 2nd, 10:16 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139