CI_URI::segment()

segment($n[, $no_result = NULL])

Parameters:
  • $n (int) – Segment index number
  • $no_result (mixed) – What to return if the searched segment is not found
Returns:

Segment value or $no_result value if not found

Return type:

mixed

Permits you to retrieve a specific segment. Where n is the segment number you wish to retrieve. Segments are numbered from left to right. For example, if your full URL is this:

http://example.com/index.php/news/local/metro/crime_is_up

The segment numbers would be this:

  1. news
  2. local
  3. metro
  4. crime_is_up

The optional second parameter defaults to NULL and allows you to set the return value of this method when the requested URI segment is missing. For example, this would tell the method to return the number zero in the event of failure:

$product_id = $this->uri->segment(3, 0);

It helps avoid having to write code like this:

if ($this->uri->segment(3) === FALSE)
{
        $product_id = 0;
}
else
{
        $product_id = $this->uri->segment(3);
}
doc_CodeIgniter
2016-10-15 16:32:02
Comments
Leave a Comment

Please login to continue.