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

[eluser]sl3dg3hamm3r[/eluser]
hey there

I have a trivial problem, but can't see what's wrong:

Code:
if (isset($_POST['cancel']))
  echo "Test 1";
if ($this->input->post('cancel'))
  echo "Test 2";

I get only 'Test 1' as an output. Why?

Sl3dg3
#2

[eluser]Murodese[/eluser]
$this->input->post() returns false if the field is unset or empty. (due to the way php handles blank variables as false)

isset() returns false if the field is unset, but will return true if it's set but empty.

(see below for a solution)
#3

[eluser]manilodisan[/eluser]
if (isset($_POST['cancel']))
echo "Test 1";
if ($this->input->post('cancel') != FALSE)
echo "Test 2";
#4

[eluser]Murodese[/eluser]
[quote author="manilodisan" date="1224352492"]if (isset($_POST['cancel']))
echo "Test 1";
if ($this->input->post('cancel') != FALSE)
echo "Test 2";[/quote]

Code:
if (isset($_POST['cancel']))
  echo "Test 1";
if ($this->input->post('cancel') !== FALSE)
  echo "Test 2";

Missed an =
#5

[eluser]sl3dg3hamm3r[/eluser]
[quote author="Murodese" date="1224352823"]Missed an =[/quote]

That's exactly what I did wrong while I was trying the same thing with false.

Many thanx!
#6

[eluser]ray73864[/eluser]
i never understood the need for the extra ='s, i always use != or == and it always seems to work, so why the extra = sign?
#7

[eluser]Murodese[/eluser]
[quote author="ray73864" date="1224420001"]i never understood the need for the extra ='s, i always use != or == and it always seems to work, so why the extra = sign?[/quote]

PHP defines several things as false. FALSE, 0, "", null or an empty array will all qualify as false. Adding the extra = (ie === or !==) will make sure it also type-checks it to make sure it's actually FALSE and not one of the others.

Code:
if (0 == FALSE) // true
if (0 === FALSE) // false
#8

[eluser]manilodisan[/eluser]
'===' forces the condition to also check the type of value (string,integer,boolean,array etc.):

Code:
1 === '1'; //false since '1' is string and not integer
//while
1==TRUE; //returns true
1===TRUE;// returns false since the type of these values is not the same (integer/boolean)




Theme © iAndrew 2016 - Forum software by © MyBB