Home » Php » php – optional parameters in CodeIgniter

php – optional parameters in CodeIgniter

Posted by: admin July 12, 2020 Leave a comment


I’m trying to write a function in a CodeIgniter controller which can take optional parameters. However, I always get Missing Argument warnings. I’m not trying to suppress the warnings – I’m trying to declare the parameters as optional (maybe they could be empty strings if they don’t exist, or something).

What is it I’m missing?


How to&Answers:
  public function my_optional_test($not_optional_param, $optional_param = NULL)
  { $this->stuff(); }

have you tried this?


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

  1. example.com/index.php/mycontroller/myfunction/hello/world
  2. example.com/index.php/mycontroller/myfunction/hello

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

class MyController 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.


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