使用if语句默认未设置的值

I think there a easier way to perform the same function but without if statements.

if (isset($_GET['limit']))
 {
  $limit = $_GET['limit'];
 }
else
 {
   $limit = 10;
 }

If there is I'd be open to hearing it...

...But I think your best bet is just a short hand if/else statement instead. Not that it really makes any difference.

$limit = (isset($_GET['limit']) ? $_GET['limit'] :  10;

More info here - http://www.lizjamieson.co.uk/9/short-if-statement-in-php/

$limit = isset($_GET['limit']) ? $_GET['limit'] : 10;

short if ?

$limit = isset($_GET['limit']) ? $_GET['limit'] :  10;
$limit = $_GET['limit'] ?: 10;

This assumes PHP 5.3(?) or higher, though.

You can use ternary comparison operator:

$limit = (isset($_GET['limit'])) ? $_GET['limit'] : 10;

You can write the shorter ?: expression

$limit = isset($_GET['limit']) ? $_GET['limit'] : 10;

Or better yet just write your own function

$limit = updateMyGetVar('limit', 10);

function updateMyGetVar($var, $default = NULL){
  if (isset($_GET[$var]))
  {
    return $_GET[$var];
  }
  else
  {
    return $default;
  }
}