此php代码行的含义是什么? 代码点火器

问题描述

| 因此,今天我玩了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);
}
    ,        如果已设置,则将发布变量\“ id \”绑定到$ id。否则,请使用第三个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-> input-> get_post(\'id \') 表示您与另一个“输入”类一起位于一个对象(类)中,并使用方法get_post()。 这个:(int)x 将x转换为int。 如果get_post()不同于0或\“ \”,他选择如何分配id使用uri-segment(3)的值