comparing numbers

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
Craig
Forum Commoner
Posts: 37
Joined: Thu Apr 18, 2002 3:13 pm

comparing numbers

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Craig
Forum Commoner
Posts: 37
Joined: Thu Apr 18, 2002 3:13 pm

Post by Craig »

wowser, so easy, thanks it works (of course ;) ) and that'll put meon the right track

And happy 1000th post
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Cool and thanks.

Mac
Craig
Forum Commoner
Posts: 37
Joined: Thu Apr 18, 2002 3:13 pm

Post 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?
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post 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
Post Reply