I wrote a user function to return the distance between two points on an x,y coordinate system. The input params were 0,0,10,10
.
Here is the original code:
public static function dist2d($x1,$y1,$x2,$y2) {
return sqrt((($x2 - $x1) * ($x2 - $x1)) + (($y2 - $y1) * ($y2 - $y1)));
}
This returns 110.
Here is the code that works:
public static function dist2d($x1,$y1,$x2,$y2) {
$result = (($x2 - $x1) * ($x2 - $x1)) + (($y2 - $y1) * ($y2 - $y1));
return sqrt($result);
}
This returns 14.1
.
I am new to PHP, what is going on here?
这两个函数都返回14.142135623731。
Are you sure about that? It's pretty strange because the two functions are identical.
I tried both of them in my environment (using cut & paste from your question) and both return 14.142135623731.
你的PHP的特定版本一定是奇怪的,因为我的工作正常(5.3.3)。
You have probably made some kind of typo - both versions work for me correctly (please see http://codepad.org/1gFCTPfU as evidence).
Plus, if you are not describing static method of some class, you should replace public static function dist2d(
with function dist2d(
, otherwise you will get parse error as here: http://codepad.org/GkMfFqlo
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.