CodeIgniter Forums
Default parameters for controller functions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Default parameters for controller functions (/thread-26651.html)



Default parameters for controller functions - El Forum - 01-19-2010

[eluser]chaotiq[/eluser]
Can I give a default parameter for controller functions and then override with a uri?

Consider the following code.
Code:
Class My_controller extends Controller{

    function my_function($var = 0)
    {
        if ($var == 0)
        {
            //do stuff
        }
        else
        {
            //do other stuff
        }
    }
}

If I call the function without giving it a parameter the first part of the if statement works as intended. However, if I try to go to:

http://mysite.com/index.php/my_controller/my_function/213

I would expect the 213 to override the default of 0 and the else should run. When I try this the first part of the if statement still runs.

Am I thinking about this wrong?

[Edit: formatting]


Default parameters for controller functions - El Forum - 01-19-2010

[eluser]danmontgomery[/eluser]
Works for me:

Code:
class test extends Controller {
    
    public function my_function($var = 0)
    {
        echo "\$var is " . $var;
    }

}

/test/my_function

Quote:$var is 0

/test/my_function/213

Quote:$var is 213



Default parameters for controller functions - El Forum - 01-19-2010

[eluser]chaotiq[/eluser]
It does work. Thank you for testing it and your time. I misspelled one letter and one of my functions was not returning the right data. I feel ashamed. :roll:

Thanks again for testing it out.


Default parameters for controller functions - El Forum - 01-19-2010

[eluser]danmontgomery[/eluser]
Wink