Page 1 of 1

comparing numbers

Posted: Wed Sep 18, 2002 4:30 am
by Craig
eek!

I've never needed to do this so i've never thought about it, so bear with me if I'm talking crap.

How would I/or can I compare numbers

For example I have the number 4 in a variable and a second number ($newnumber) how would I check the value of $newnumber and output the difference

ie

$firstnumber = 4
$newnumber = 6

I want to get the output to be "+2" actually showing the plus sign, likewise if $newnumber had been 3 the output should show "-1"

Any help or a point in the right direction for further reading would be gratefully accepted thanks.

Posted: Wed Sep 18, 2002 4:35 am
by twigletmac

Code: Select all

<?php 
$firstnumber = 4; 
$newnumber = 6; 
$dif = $newnumber - $firstnumber; 
echo $newnumber.' - '.$firstnumber.' = '.$dif; 
if ($dif > 0) { 
    $dif = '+'.$dif; 
} 
echo '<br />Therefore the difference is: '.$dif; 
?>
Hope it helps,

Mac

Posted: Wed Sep 18, 2002 4:42 am
by Craig
wowser, so easy, thanks it works (of course ;) ) and that'll put meon the right track

And happy 1000th post

Posted: Wed Sep 18, 2002 4:45 am
by twigletmac
Cool and thanks.

Mac

Posted: Wed Sep 18, 2002 5:18 am
by Craig
Is there anywhere I can find a hand tutorial on foreach loops?

I have a list of numbers in one table and I'll be inputing numbers through a form which will be compared against the ones in the table before being added to their own table. I assume that foreach would be the way to do it

something like this I'd guess:

Code: Select all

foreach (something as something etc blah blah){
  
$dif = $newnumber - $firstnumber;  
echo $newnumber.' - '.$firstnumber.' = '.$dif;  
if ($dif > 0) {  
    $dif = '+'.$dif;  
}  
echo '<br />Therefore the difference is: '.$dif;  

&#125;
something along this track would load each one into an array wouldn't it and I'd be able to access each compared number by

$newnumber[1];
$newnumber[2];

or something like that I think.

Is this the right line of thought?

Posted: Wed Sep 18, 2002 11:53 am
by gite_ashish
hi,

see this url, it's php online man with examples & usr contributed notes:

http://www.php.net/manual/en/control-st ... oreach.php