CodeIgniter Forums

Full Version: Post vars problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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

El Forum

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

El Forum

[eluser]RecoilUK[/eluser]
Thanks

But that still works the same.

El Forum

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

That works though.

Thanks

El Forum

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

El Forum

[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.

El Forum

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