Simple PHP calculating program troubles

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
Silentfury45
Forum Newbie
Posts: 1
Joined: Sun Feb 15, 2015 1:22 pm

Simple PHP calculating program troubles

Post by Silentfury45 »

Hey I'm working on a simple program that will take user input from a HTTP form and use that to calculate a few things. I a few problems with my counter and I was wandering if anyone could point out something that could be causing my problem?



Also if anyone know how to properly get a count on the positive even int's???



This is what I'm working with.

Code: Select all

$count = 0;
$PosCount = 0;
 $uname = array($uname1, $uname2, $uname3, $uname4, $uname5, $uname6, $uname7, $uname8);
 
for ($i; $i < count($uname); ++$i) {

if ($uname[$i] == '0' || $uname[$i] == null) { //look to see weather its good or bad
unset ($uname[$i]); //break that empty field
$count = ($count - 1);

}
 
 $count = $count + 1;
 
 if ($uname[$i] % 2 == 0 ) {//Even check

$i = $uname[$i];
if ($i == round($i) || $i < 1) { //Pos Check
$PosCount = $PosCount + 1;
        }
    }

}


than of course echoing all my desired values (sum, avg, max, min, and pos int count)



- Thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Simple PHP calculating program troubles

Post by requinix »

I'm not even 100% sure what it's trying to do.

Code: Select all

$uname = array($uname1, $uname2, $uname3, $uname4, $uname5, $uname6, $uname7, $uname8);
I assume you already got those $unameX values from $_POST earlier?

Code: Select all

for ($i; $i < count($uname); ++$i) {
1. You need to initialize $i=0. Saying just "$i" does nothing and leaves it at its default value of null.

Code: Select all

unset ($uname[$i]);
This will remove an item from the array and thus its count() will be one less. However the other items in the array do not change their keys: if you unset $uname(3) then the array will contain 0,1,2,4,5,6,7 and $i will only go up to 6.
Either (a) do not remove items from the array or (b) make your loop count to 7 regardless by storing count($uname) beforehand or by simply using the value 7.

Code: Select all

$i = $uname[$i];
This will mess up your loop by changing $i to whatever value came from the form. When the loop resumes at the beginning, $i will increment its current value - not continue from where it left off at the beginning of the previous iteration.

Code: Select all

if ($i == round($i) || $i < 1) {
I don't know what this is for.
Post Reply