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.
comparing numbers
Moderator: General Moderators
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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;
?>Mac
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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:
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?
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;
}$newnumber[1];
$newnumber[2];
or something like that I think.
Is this the right line of thought?
- gite_ashish
- Forum Contributor
- Posts: 118
- Joined: Sat Aug 31, 2002 11:38 am
- Location: India
hi,
see this url, it's php online man with examples & usr contributed notes:
http://www.php.net/manual/en/control-st ... oreach.php
see this url, it's php online man with examples & usr contributed notes:
http://www.php.net/manual/en/control-st ... oreach.php