Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

PHP Help


ghost's Avatar
0 0

I've been learning PHP for a while now and am just going over everything just so I know all the functions and how to use them for sure. I have already encountered a problem in one of my scripts. I was just making a simple counter and wanted to put bounds on the number your allowed to put in (In the script its supposed to only allow everything from 1-999). The only problem I have is when I try to print out the error for putting in a zero (I just remembered I shouldn't allow negatives >_<) it doesn't show anything. Can someone show me what I'm doing wrong so I can get this down perfectly? I can't seem to spot the error.

// user generated counter script
$count=1;
$random=$_GET[&quot;count_num&quot;];
for ($num=$_GET[&quot;count_num&quot;]; $count&lt;=$num; $count++) {
	if ($random==0) {
		echo &quot;Please input an allowed number. Your input: &quot;.$_GET[&quot;count_num&quot;].&quot; was not acceptable.&quot;;
		break;
		}
	elseif ($num&gt;=1000) {
		echo &quot;Please input an allowed number. Your input: &quot;.$_GET[&quot;count_num&quot;].&quot; was not acceptable.&quot;;
		break;
		}
	echo $count;
	echo &quot;&lt;br /&gt;&quot;;
}

?&gt;

I know it's kinda bad coding, I've only been learning PHP for like 2 weeks now or so. I put in the $count=1; because without that it would have a blank spot when I put in any number and then it would continue on counting. So please help me out! Thanks, Kirk.


ghost's Avatar
0 0

the problem is with this line:

markupfor ($num=$_GET[&quot;count_num&quot;]; $count&lt;=$num; $count++) {

if the input is 0, it won't execute the block following this line at all. if $count is 1 and $count is supposed to be <= $num, and if $num is 0 the check will return false, thus the loop won't execute.

I'd do the check before you do the loop, something like this:

if ($num == 0 || $num &gt;= 1000)
 echo &quot;Invalid input, your input was &quot; . $num;

//loop here