Tutorial specifically to learn this "?" and ":" stuff

Discussion in 'PHP' started by goknicks, Jul 31, 2012.

  1. #1
    Shamefully I have never moved past the n00b state. I need a tutorial to figure out how to understand the following to the point where I can actually create this without having to look anything up:

    
    <!-- Before loop --> <?php $c = 0; ?>  <!-- Start loop --> <tr class="<?=($c++%2==1) ? 'odd' : 'even' ?>"> <!-- End loop -->
    
    PHP:

     
    goknicks, Jul 31, 2012 IP
  2. amgadhs

    amgadhs Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
  3. nveid

    nveid Peon

    Messages:
    155
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Really not sure what type of tutorial your talking about this is very simple. Is it truly the ternary operator your having issues with, or the modulus operator?

    In anycase, here we go.

    ?: is also known as the ternary operator, works on the simple operation of <Your boolean expression ? <if true> : <if false>

    For example write a simple script that does
    
    <?
      echo true ? "This is True" : "This will never happen";
    ?>
    
    Code (markup):
    Now, you also had in there the example of a modulus operator %.. this is thing of is the number evenly divisible by such & such? If so it will return 0, if not it will return true.

    Some examples
    
    1 % 1 = 0
    1 % 2 = 1
    2 % 2 = 0
    4 % 2 = 0
    5 % 2 = 1
    
    Code (markup):
    So you can use these modulus operators anywhere you want in order to get the affect you want. Also since your a beginner thinking I'll make you realize how the increment operator ACTUALLY works.. Sometimes people have the wrong idea, they think as soon as they do $i++ its going to get the new value. That is not the case.

    In fact,
    
    <?
     $i = 0;
     echo $i++ . "\r\n";
     echo $i;
    ?>
    
    Code (markup):
    This will return respectively,
    0
    1

    While we're on it.. Whats difference between <?= $string ?>, <?php echo $string ?>, and <? echo $string ?>.. Nothing! :) They're all equivalents, only different is the last one in there is using shorthand syntax, the first is using shorthand echo syntax(which is available on al php installation I think as of php 5.4 :). Hope this has all helped you out a little.
     
    nveid, Aug 1, 2012 IP
  4. PHPlancer

    PHPlancer Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ternary operator can also be nested.

    Example:

    
    $value = $condition1 ? $value1 : ($condition2 ? $value2 : $value3);
    
    PHP:
    The example translates to:

    
    if($condition1) {
      $value = $value1;
    }else if($condition2) {
      $value = $value2;
    }else {
      $value = $value3;
    }
    
    PHP:
    Also Ternary operator always returns a value.
     
    PHPlancer, Aug 1, 2012 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    You can have it return an empty value though.

    My advice is to use Ternary sparingly and only at the appropriate time. It certainly reduces the length of php code, but it often does so at the expense of readability. Nesting can get really ugly.
     
    jestep, Aug 1, 2012 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,814
    Likes Received:
    4,535
    Best Answers:
    123
    Trophy Points:
    665
    #6
    I usually do it slightly longer but easier to understand, I reckon

    
    $toggle = 1;
    foreach($mydata as $row) {
      $class = ($toggle)?'odd':'even';
      echo "<td class='{$class}'>"; 
      $toggle = 1 - $toggle;
    }
    PHP:
     
    sarahk, Aug 1, 2012 IP
  7. nveid

    nveid Peon

    Messages:
    155
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thats not going to do the trick, use the modulus operator.
    
    try this instead 
    $count = 1;
    foreach($mydata as $row) {
    echo '<td class="'.(!($count++ % 2) ? "even" : "odd").'">';
    }
    
    PHP:
    Notice I'm using a NOT operation here as well.. So the actual boolean value is inverses every time.
    Whats this doing first we're going to start with 1
    So first operation is
    (1 % 2) = 1, which we're inverting that value with the NOT operator !, which then evaluates to !(1 % 2) = 0.. So we get odd
    next operation is
    !(2 % 2) = 1, so we get even.. And this pattern repeats itself.
     
    Last edited: Aug 1, 2012
    nveid, Aug 1, 2012 IP
  8. sarahk

    sarahk iTamer Staff

    Messages:
    28,814
    Likes Received:
    4,535
    Best Answers:
    123
    Trophy Points:
    665
    #8
    It may not be the way you prefer to do it but it does work :)
     
    sarahk, Aug 1, 2012 IP
  9. nveid

    nveid Peon

    Messages:
    155
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Oh i see, just ran that code.. Nifty I guess I didn't look that into too much at first sight.
    
    1 - 1
    1 - 0
     
    Code (markup):
    I see it does work, just never seen it done that way in my decades of coding. Guess I see and learn something new every day. :)
     
    nveid, Aug 1, 2012 IP
  10. sarahk

    sarahk iTamer Staff

    Messages:
    28,814
    Likes Received:
    4,535
    Best Answers:
    123
    Trophy Points:
    665
    #10
    Its the way we got taught to do it when I was working on big Oracle systems. Seemed straight forward so I use it in PHP too.
     
    sarahk, Aug 2, 2012 IP
  11. PHPlancer

    PHPlancer Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Dry run codes as you see instead of actually running them. :)
     
    PHPlancer, Aug 2, 2012 IP
  12. goknicks

    goknicks Greenhorn

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #12
    Thanks for the links.
     
    goknicks, Aug 3, 2012 IP
  13. goknicks

    goknicks Greenhorn

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #13
    Super clear explanation. Thanks!
     
    goknicks, Aug 3, 2012 IP