Why does this return false?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Why does this return false?

Post 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?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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;'])
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

didn't work

Post 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...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

did you use 0 as test-value?

Code: Select all

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