Welcome Guest, Not a member yet? Register   Sign In
$_POST and $this->input->post()
#1

[eluser]Kevin_3_57[/eluser]
Hi everybody, I was hoping if someone could enlight me a little.

When using one of the functions of codeigniter, the one to retrieve POST data, I came up with an error that $_POST doesn't make.

This right here, throws this: Fatal error: Can't use method return value in write context in about.php on line 717
Code:
if(isset($this->input->post("id_master"))) $data2["master"] = "1";

But, if I replace codeigniter's default function to retrieve post data, to $_POST:
Code:
if(isset($_POST['id_master'])) $data2["master"] = "1";

This works absolutely fine.

So, I really would like to stick to CodeIgniter helpers and functions... so why is this happening? What's wrong with this?

Thanks very much in advanced.
Kevin
#2

[eluser]davidbehler[/eluser]
Code:
$this->input->post("id_master")
is a call to the post method of the input library. Method calls return a value, in this case FALSE if the supplied index doesn't exist in the $_POST array and the actual value from the array if the index was found.

You might want to read the guide: http://ellislab.com/codeigniter/user-gui...input.html

Anyway, to make your example work you have to do something like this:
Code:
if($this->input->post("id_master") !== FALSE) $data2["master"] = "1";
#3

[eluser]KingSkippus[/eluser]
waldmeister is right, no need to expand on his explanation.

However, I did notice you use the code:

Code:
if (isset($_POST['id_master'])) $data2['master'] = '1';

to test if the key 'id_master' exists in $_POST. If you are testing for the existence of an array key, the technically correct way to do so is to use the array_key_exists() function instead. In this case, it would be:

Code:
if (array_key_exists('id_master', $_POST)) $data2['master'] = '1';

The reason why is a fringe scenario that could bite you some day and cause a lot of banging of your head on a wall trying to debug a very nuanced difference if you use your version of the test, although it's highly unlikely specifically on the $_POST variable. That is the case where you have assigned an array key the value of NULL. (e.g. $foo['monkey']=NULL, or $foo['monkey']=some_possibly_null_function()) In this case, isset will return FALSE, whereas array_key_exists will return TRUE. Likewise, if you want to remove an array element, use unset(), not setting it to NULL.
#4

[eluser]Kevin_3_57[/eluser]
Thank you very much guys! Now I really understand where my mistake was. Thanks very much!.




Theme © iAndrew 2016 - Forum software by © MyBB