SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Feb 2003
    Location
    Montesano, WA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What does this line of php mean?

    Can someone please describe what this line of code means?
    Code:
     $number  = ($number) ? $number : "10";
    Is there an alternate way to write it?
    puppyfetch - under construction

  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (!isset($number)) $number 10
    $number is (if $number exists return $number else return 10)
    No, I wouldn't believe you if you said you understood that.

    From the PHP manual
    There is one more expression that may seem odd if you haven't seen it in other languages, the ternary conditional operator:

    <?php
    $first ? $second : $third
    ?>

    If the value of the first subexpression is TRUE (non-zero), then the second subexpression is evaluated, and that is the result of the conditional expression. Otherwise, the third subexpression is evaluated, and that is the value.

  3. #3
    Non-Member
    Join Date
    Nov 2002
    Location
    Earth
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's the the ternary conditional operator - shorthand for if/else.

    http://www.php.net/manual/en/language.expressions.php

  4. #4
    SitePoint Member
    Join Date
    Feb 2003
    Location
    Montesano, WA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the insight... it's starting to make sense.
    puppyfetch - under construction


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
  •