• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post vars problem

#1
[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

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

#3
[eluser]RecoilUK[/eluser]
Thanks

But that still works the same.

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

That works though.

Thanks

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

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

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.