所以今天我用http://bambooinvoice.org/源代码和我发现这行:
$id = ($this->input->get_post('id')) ? (int) $this->input->get_post('id') : $this->uri->segment(3);
我已经理解了codeigniter中使用的基本语法,但希望有人能告诉我这个符号(?)在这两种语法之间有什么用途?如果它是某种技术,那么该技术的名称是什么?他想用这行代码实现什么?
谢谢。
Ternary
运算符;与相同
if(($this->input->get_post('id')) == true)
{
$id =(int) $this->input->get_post('id')
}
else
{
$id=$this->uri->segment(3);
}
将post变量"id"绑定到$id if,if已设置。否则,使用第三个url段的值。
这是的快捷方式
if($this->input->get_post('id'))
$id = $this->input->get_post('id');
else
$id = $this->uri->segment(3);
它是一个三元运算符:语法:
$id = (condition) ? value_when_condition_is_true : value_when_condition_is_false;
$id=($this->input->get_post('id'))?(int)$this->input->get_post('id'):$this-->uri->segment(3);
这个:z=(x>y?x:y);类似于:
if (x > y)
{
z = x;
}
else
{
z = y;
}
this:$this->input->get_post('id')
意味着您在一个对象(类)中,有另一个类"input",并使用方法get_post()。
这个:(int)x将x转换为内部
他选择如何分配id,如果get_post()不同于0或"使用uri段(3)的值