eri
eri

Reputation: 119

Does anyone know what this line of code means?

I have this line of code:

    $data['page'] =($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

I am familiar with ternary operations, but I am not very sure what this line of code does.

Upvotes: 0

Views: 84

Answers (4)

TimBrownlaw
TimBrownlaw

Reputation: 5507

$value = <test condition>? value to use if True, : value to use if False

In the case of

$data['page'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

As per the userguide ( we all read that now and then... ), $this->uri->segment(3) will return the value of segment 3 of the uri if it exists, else it will return NULL.

/controller/method/parameter Segment 1 = controller Segment 2 = method Segment 3 = parameter , and we can have any number of parameters after that, so you could have more segments..

So if the uri contains a Segment 3 ( ie /controller/method/parameter ), the "parameter" value would be returned as it's the 3rd segment and in the above test, it will evaluate to be Not NULL or True and $data['page'] will be set to the value defined after the ? which in this case, is itself, i.e the value of $this->uri->segment(3).

If the uri does not contain a segment 3 (ie /controller/method ), then $this->uri->segment(3) will evaluate to NULL or False and the value after the : is assigned to $data['page'], in this case 0.

So this is how you can return any value you like instead of NULL in this instance.

As per the userguide for CI 3.1.0 you can pass a 2nd parameter to perform the same task.

So instead of

$data['page'] =($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

You use

$data['page'] = $this->uri->segment(3,0);

Now as this example appears to be passing what appears to be a page name as the 3rd segment of the uri, going to all the trouble to alter the default or failed value from Null to 0 seems iffy.

You'll be wanting to test for this... i.e do I get a page name or don't I. If I do, I'll get a string, so then I can see if it matches a page name I have. If I don't get a page_name, ie nothing, I'll get a Null and when I test for that I'll do something appropriate...

If that's even more confusion I apologize!

Upvotes: 0

frame
frame

Reputation: 124

$this->uri->segment(3) seems to return a page number, if possible.

The condition checks if the result is true (a valid number > 0 is also true) and assigns the result to $data['page']. If the result in unexpected or invalid (most likely false or null in this scenario) 0 gets applied instead.

This ensures that $data['page'] always gets a proper value.

You could also write this:

$data['page'] = 0;
if (is_numeric($this->uri->segment(3)) && $this->uri->segment(3) > 0) {
    $data['page'] = $this->uri->segment(3);
}

Upvotes: 0

Franz Gleichmann
Franz Gleichmann

Reputation: 3568

well, if $this->uri->segment(3) evaluates to something that isn't false, then $data['page'] will be set to its content, otherweise to 0.

you'd know that if you actually knew ternary operations, as you stated.

it's a very common way to establish a default value for a variable.

Upvotes: 0

Battle Mage
Battle Mage

Reputation: 379

if ($this->uri->segment(3)){
    $data['page'] = $this->uri->segment(3);
} else {
    $data['page'] = 0;
}

Upvotes: 1

Related Questions