What does this mean?

Discussion in 'PHP' started by qwikad.com, Jul 1, 2017.

  1. #1
    
    $page = $_GET['page'] ? $_GET['page'] : 1;
    
    Code (markup):

     
    Solved! View solution.
    qwikad.com, Jul 1, 2017 IP
  2. #2
    Not sure what you mean?
    What you have there is a ternary operator (basically a short version of an if/else statement).
    However, it's not well formed.
    You have a condition (if there is a $_GET['page'] then use the value, if there isn't, use 1). It's the same as writing
     
    if ($_GET['page']) {
      $page = $_GET['page'];
    } else{
      $page = 1;
    }
    
    Code (markup):
    However, there's no check to see if the $_GET['page'] actually exist, so depending on error reporting, that will throw a warning.

    Besides, with the new PHP versions, the simpler way would be:
     
    $page = $_GET['page'] ?? 1;
    
    Code (markup):
     
    PoPSiCLe, Jul 1, 2017 IP
    JEET, sarahk and qwikad.com like this.
  3. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    @PoPSiCLe Thanks for the detailed explanation. I too learnt something new! :) Thanks
     
    JEET, Jul 6, 2017 IP