Reputation: 5858
So today i play with the http://bambooinvoice.org/ source code and i found this line:
$id = ($this->input->get_post('id')) ? (int) $this->input->get_post('id') : $this->uri->segment(3);
I already understand the basic syntax use in codeigniter but hope someone can tell me what is the use of this symbol (?) between the two syntax? If it some kind of technique, what is the name of the technique? What is he trying to achieve with this line of code?
Thanks.
Upvotes: 0
Views: 757
Reputation: 103
$id = ($this->input->get_post('id')) ? (int) $this->input->get_post('id') : $this->uri->segment(3);
this: z = ( x > y ? x : y ); is like:
if (x > y)
{
z = x;
}
else
{
z = y;
}
this: $this->input->get_post('id')
mean you are in an object (class), with an other class "input" and use the methods get_post().
this: (int) x cast x as int.
He choose how assign id, if get_post() is different than 0 or "" use the value of uri-segment(3)
Upvotes: 0
Reputation: 9986
Ternary
operator; same as
if(($this->input->get_post('id')) == true)
{
$id =(int) $this->input->get_post('id')
}
else
{
$id=$this->uri->segment(3);
}
Upvotes: 2
Reputation: 8760
It is a shortcut of these:
if($this->input->get_post('id'))
$id = $this->input->get_post('id');
else
$id = $this->uri->segment(3);
It is a ternary operator: Syntax:
$id = (condition) ? value_when_condition_is_true : value_when_condition_is_false;
Upvotes: 1
Reputation: 16060
Bind post Variable "id" to $id if, it is set. Otherwise use the value of the third url-segment.
Upvotes: 1