Page 1 of 1

Why does this return false?

Posted: Wed Feb 05, 2003 6:04 pm
by uberpolak
I have a dynamic form, which generates fields like so:

Code: Select all

<?php

for ($z = 1; $z <= $_POST['items']; $z++)
{
 echo "<P ALIGN="left">";
 echo "<B>Barcode for item #" . $z . ":</B> ";
 echo "<INPUT TYPE="text" NAME="b1_" . $z . "">-";
 echo "<INPUT TYPE="text" NAME="b2_" . $z . "">-";
 echo "<INPUT TYPE="text" NAME="b3_" . $z . ""></P>";
}

?>
It also passes a hidden field with the number of items it's sending. On the script it calls, I check that all these fields are filled out with this function:

Code: Select all

<?php

function checkbar($items)
{
 for ($z = 1; $z <= $items; $z++)
 {
  if (${"b1_" . $z})
  {
   if (${"b2_" . $z})
   {
    if (${"b3_" . $z}) {$pass = true;}
    else {$pass = false; break;}
   }
   else {$pass = false; break;}
  }
  else {$pass = false; break;}
 }
 return $pass;
}

?>
In testing with 1 item, it has always returned false, even though all three fields are filled out. I tried just echoing each of the three values, which worked fine, and register_globals is on. I'm completely stumped and may have to give up my "geek" stauts at work... any ideas?

Posted: Wed Feb 05, 2003 8:40 pm
by hob_goblin
it's not finding the b1_ variable...

use $_POST, it's a superglobal

ie:

Code: Select all

if ($_POST&#1111;'&#123;"b1_" . $z&#125;'])

Posted: Wed Feb 05, 2003 10:02 pm
by Stoker
.. and take a look in the manual about variable scope, functions do not not automatically use global vars without using the superglobal $GLOBALS or calling global $var

didn't work

Posted: Thu Feb 06, 2003 2:08 pm
by uberpolak
That change didn't help. I've been able to echo the variables fine so PHP knows of their existence, for some reason within the function they are coming up false, I tried declaring them global within the function...

Posted: Thu Feb 06, 2003 11:49 pm
by volka
did you use 0 as test-value?

Code: Select all

$i = "0";
if (! $i )
	echo 'is false';
will print is false