whats wrong with this logic

Associate
Joined
19 Jul 2006
Posts
1,847
PHP:
$cart = unserialize(stripslashes($_COOKIE["basket"]));
	$test = false;
while ( list( $item, $qty ) = each($cart) )
		{ 
		$num = (int)$qty;
			if ($num >= 1);
			{
			$test = true;
			break;
			}
		}
		if ($test == false)
		{
		
		echo 'theres noting in the cart';
		}

what in trying to do is get something out of an associated array and check to see if any of the quantity is greater than or equal to 1 or print the error message.

but it $test always seems to be true even when all qty is less than 1
 
I think its because you have an extra ; in your code. Try this instead

PHP:
if ($num >= 1) {
   $test = true;
   break; 
}
 
That's correct. The ; at the end of the if() line will be taken to be the end of the if() block and the {} parenthesised block afterwards will just be executed as normal, leading to $test always being set to true. I'd actually expect a syntax error, but admittedly I've never tried this layout myself.
 
Thank you so much, that bloody ; was the problem

Happens to the best of us, that and forgetting to put double '=' in if tests and the like.

Even in C++ with an IDE, the warning from the compiler can get buried among the other several hundred warnings...
 
Back
Top Bottom