CodeIgniter Forums
Post vars problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter Archived (from EllisLab) (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Post vars problem (/thread-54379.html)



Post vars problem - El Forum - 09-06-2012

[eluser]RecoilUK[/eluser]
Hi guys

I have a piece of code that I am having issue's with ...

post['char'] can be anything in the range of A-Z and 0-9

Code:
if(!$this->input->post('char') === FALSE) {
   $character = $this->input->post('char');
  } else {
   $character = "A";
  }

Now the problem comes when post['char'] is zero, as it validates to FALSE and then sets $character as A.

How can I stop this?

Any help would be appreciated.

Thanks


Post vars problem - El Forum - 09-06-2012

[eluser]Massaki[/eluser]
Code:
$character = $this->input->post('char');
if (!$character) $character = "A";



Post vars problem - El Forum - 09-06-2012

[eluser]RecoilUK[/eluser]
Thanks

But that still works the same.



Post vars problem - El Forum - 09-06-2012

[eluser]RecoilUK[/eluser]
Code:
$character = $this->input->post('char');
  if ($character === FALSE) {
   $character = "A";
  }

That works though.

Thanks


Post vars problem - El Forum - 09-06-2012

[eluser]Massaki[/eluser]
Sorry, try this:
Code:
$character = $this->input->post('char');
if (is_null($character)) $character = "A";



Post vars problem - El Forum - 09-06-2012

[eluser]CroNiX[/eluser]
@Massaki

input::post() will return a boolean FALSE if the POST key doesn't exist, so using is_null() is useless as it will never be null. It will either have the value from the POST array or be boolean FALSE. This will be changing in the future, but not in v2.x.

So checking with === FALSE is the proper way; not checking for evaluating to false (!$character), which a 0 value will do too.



Post vars problem - El Forum - 09-06-2012

[eluser]Aken[/eluser]
Note that $this->input->post() (and similar methods) WILL return null as of CI 3.0, whenever it is released.