how arguments are passed to codeigniter method how arguments are passed to codeigniter method codeigniter codeigniter

how arguments are passed to codeigniter method


I don't know if this is a bug or a expected behavior, but in the Strings docs there's a comment that show exactly what are you experiencing. If you use a text and index of the string it will return the first char. To avoid it, check first if the argument is an array or a string:

if(is_array($args)) {    echo($args['first_name']);}


To complete @SérgioMichels answer, the reason for that is because PHP is expecting an integer as the given index. When you give it a string, PHP will cast the string into an integer, and assuming that the string does not start with a number, type casting will return 0 otherwise, it will return the leading number.

$str = 'abcdefghi';var_dump($str['no_number']); // Outputs: string(1) "a"var_dump($str['3something']); // Outputs: string(1) "d"


To specifically answer your question - this will solve your bug:

function get_details($args='') {    if (is_array($args))    {        $first_name = $args['first_name'];    }     else    {        $first_name = $this->uri->segment(3);    }     ... do some other stuff ...}

But you have some issues with your code. Firstly you state that you call the method as

<domain>/<controller>/get_details/abcd/efgh 

but you dont accept the "efgh" variable in your controller. To do this, you need to change the function to

function get_details($first, $last) 

in which case you can now just call the function as

$this->get_details('abcd', 'efgh');

and now you dont even need to test for arrays etc, which is a better solution IMO.

If you decide to stick with arrays, change:

$first_name = $this->uri->segment(3);

to

$first_name = $args;

because by definition - $args IS The 3rd URI segment.