PHP for Maths

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
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

PHP for Maths

Post by Parody »

Hmm....

Any ideas what is wrong with this?:

Code: Select all

<?php
$weight = 3150;
$HP = 500;
$answer = (6.9446 X ($weight / $HP)^.2841);
print "$answer";
?>
I get this error:

Code: Select all

Parse error: syntax error, unexpected T_STRING in c:\program files\apache group\Apache\htdocs\mile.php on line 4
Thanks in advance :D
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Please read chapther 15. Operators in the manual (http://www.php.net/manual). Please notice that the operator for multiplication is * instead of X.
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post by Parody »

Thanks, I'm tired :oops:
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post by Parody »

Ok, it works but its getting the wrong answer

This is the equation I am trying to use:
6.9446 X (weight / HP)^.2841

This is what I have:

Code: Select all

<?php
$weight = 3826;
$HP = 294;
$answer = (6.9446 * ($weight / $HP)^.2841);
print "$answer";
?>
According to the mathematical equation (not the php one) with the same values within weight and HP the answer is : 14.396

The PHP equation gets : 90.


I think it is the ^ . What does php interpret by this symbol?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

RTFM.
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post by Parody »

Read the F***ing Manual?

I F***ing did, lol, sorry :oops:

This is what it says:

$a ^ $b Xor Bits that are set in $a or $b but not both are set.

And that means what?

That is the only time it is refrenced throughout the manual. :cry:
Ross2376
Forum Newbie
Posts: 19
Joined: Mon May 30, 2005 12:03 am

Post by Ross2376 »

Use:

Code: Select all

<?php
$weight = 3826;
$HP = 294;
$answer = (6.9446 * pow(($weight / $HP), .2841));
print "$answer";
?>
The ^ does not mean raised to in PHP. Use the pow() function instead.

pow(base, exponent);
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post by Parody »

Thanks.
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post by wwwapu »

Parody wrote:$a ^ $b Xor Bits that are set in $a or $b but not both are set.
And that means what?
This is basic logic, dear Watson. Search for "logic circuits" or "boolean algebra"
http://www.allaboutcircuits.com/vol_4/chpt_7/7.html is quite understandable explanation on xor.
Post Reply