Optional Parameters in CodeIgniter

I am trying to write a function in a CodeIgniter controller that can take optional parameters. However, I always get Missing Argument warnings. I am not trying to suppress warnings - I am trying to declare the parameters as optional (perhaps they can be empty strings if they do not exist or something else).

What am I missing?

thanks
Mala

+6
php optional-parameters codeigniter
source share
2 answers
public function my_optional_test($not_optional_param, $optional_param = NULL) { $this->stuff(); } 

Have you tried this?

+19
source share

For example, let's say you have a URI like this:

  • example.com/index.php/mycontroller/myfunction/hello/world
  • example.com/index.php/mycontroller/myfunction/hello

Your method will be passed to URI segments 3 and 4 (hello and world):

The MyController class extends CI_Controller {

 public function myFunction($notOptional, $optional = NULL) { echo $notOptional; // will return 'hello'. echo $optional; // will return 'world' using the 1st URI and 'NULL' using the 2nd. } 

}

Link: https://codeigniter.com/user_guide/general/controllers.html

0
source share

All Articles