How arguments are passed to the codeigniter method

I have a method in a codeigniter controller that is sometimes called via a URL and sometimes called internally from another controller method. When I call it internally, I pass an array of arguments. Simplified version of the method:

(inside the controller)

function get_details($args='') {

    if (isset($args['first_name'])) 
    {
        $first_name = $args['first_name'];
    } 
    else
    {
        $first_name = $this->uri->segment(3);
    } 

    ... do some other stuff ...

}

The method is called either:

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

or from another controller function:

$this->get_details(array('first_name'=>'abcd', 'last_name'=>'efgh'));

I expected that when the method is called via url, isset ($ args ['first_name']) will be false, however it seems that the argument called in this way is. I tried to print a couple of things, and here is what I got:

print_r($args)  ---->   abcd

echo($args['first_name'])  ----> a

echo($args['whatever_index_I_use'])  ----> a  

It seems like the third url parameter is being passed to the method (using codeigniter?), But cannot decide why the array indices seem to be set, all I can think of is that php will convert the string to int, so $args['whatever_index_I_use'], becomes $args[0]??

, - codeigniter php.

, , .

.

+3
3

, , Strings docs , , . , char. , , :

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

@SérgioMichels, , PHP . , PHP , , , casting 0, .

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

- :

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

. -, ,

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

"efgh" .

function get_details($first, $last) 

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

.., IMO.

, :

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

$first_name = $args;

- $args IS URI.

0

All Articles