2
if (isset($row['product_limit'])){
    if ($row['product_limit'] != 0){
        echo $row['product_limit'];
    } else {
        echo $text_infinite;    
    }
} else {
    echo $text_infinite;
}

Is there a better way of writing this???

EDIT:

though I used the @Radu code but here is the version i used

echo (isset($row['product_limit']) 
&& $row['product_limit'] != 0) ? $row['product_limit'] : 'unlimited' ;

Not expected this many methods :) Thanks All

jimy
  • 4,848
  • 3
  • 35
  • 52
  • Wow, you really got some good results here. – DOK Jun 04 '11 at 14:15
  • 1
    the ternary operator is good if the code is short, otherwise it will become hard to read... If you prefer using 2 conditions (`isset` and `!= 0`) instead of one (`empty`), then you should consider using `if`/`else` instead of `?:`. – rid Jun 04 '11 at 14:30
  • 1
    Also take a look at this question to find out about the difference between `empty()` and `isset()`: http://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty – rid Jun 04 '11 at 14:50

4 Answers4

13

How about this?

echo empty($row['product_limit']) ? $text_infinite : $row['product_limit'];

Take a look at the documentation for empty() and the ternary conditional operator.

rid
  • 61,078
  • 31
  • 152
  • 193
1
if (isset($row['product_limit']) && $row['product_limit'] != 0){
     echo $row['product_limit'];
} else {
    echo $text_infinite;    
}
Philipp
  • 1,903
  • 2
  • 24
  • 33
0
if (!empty($row['product_limit'])){
    echo $row['product_limit'];
} else {
    echo $text_infinite;    
}

As php.net says, the following things are considered to be empty:

  • "" (an empty string)

  • 0 (0 as an integer)

and so on..

0

Maybe you can try some EXME method.

    if (isset($row['product_limit']))
    {
        $msg = $row['product_limit'];
    }
    else
    {
        $msg = "";
    }

Then you just need to output the code somewhere else by calling the echo of the $msg variable.