# 獲取和輸入變量
在控制器中獲得輸入的變量值。
例URL:http://localhost/Index/index/cid/1/id/2
使用I()方法獲取cid和id的值。
~~~
class IndexController extends Controller{
public function index(){
$cid = I('cid'); //相當于$_GET['cid']
$id = I('id); //相當于$_GET['id']
echo 'cid的值是:'.$cid;
echo '<br/>';
echo 'id的值是:'.$id;
}
}
~~~
<br/><br/>
## 默認值
如果需要在獲取URL參數值為空的時輸出一個默認值,則可使用以下I方法參數,以下方式都是可以的。
~~~
$id = I('id','默認值');
$id = I('id','def');
$id = I('id','');
$id = I('id',0);
$id = I('id',1);
~~~
<br/><br/>
## 指定獲取方式
如果需要指定獲取值是get方式或post方式,則可以使用以下格式。
~~~
//當URL: http://localhost/index.php/Index/index/id/10
$id = I('get.id',0);
//$id輸出:10
$id = I('post.id',0);
//$id輸出:0(默認值)
~~~
~~~
//當從表單提交 <input name=id value=10/> 時
$id = I('get.id',0);
//$id輸出:0(默認值)
$id = I('post.id',0);
//$id輸出:10
~~~