3v4l.org

run code in 500+ PHP versions simultaneously
<?php // distance between two 2D points $x1 = 3; $y1 = 4; $x2 = 7; $y2 = 1; $dist = hypot($x2 - $x1, $y2 - $y1); // 5.0 // why not just sqrt(($dx)**2 + ($dy)**2)? // because hypot() avoids intermediate overflow // QUICK TIP STRAIGHT FROM GAME DEV: sometimes you only want to *compare* distances, and // square root is SLOW // in this case, just compare squared values, it's equivalent! $dx1 = $x2 - $x1; $dy1 = $y2 - $y1; $dist1Sq = $dx1 * $dx1 + $dy1 * $dy1; // some other points in space... $dist2Sq = $dx2 * $dx2 + $dy2 * $dy2; if ($dist1Sq < $dist2Sq) { // squared value compared, way faster without square root! echo "Point 2 is closer to Point 1\n"; } else { echo "Point 3 is closer to Point 1\n"; }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.21, 8.5.0 - 8.5.6
Warning: Undefined variable $dx2 in /in/Aacol on line 20 Warning: Undefined variable $dx2 in /in/Aacol on line 20 Warning: Undefined variable $dy2 in /in/Aacol on line 20 Warning: Undefined variable $dy2 in /in/Aacol on line 20 Point 3 is closer to Point 1
Output for 5.0.4 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Notice: Undefined variable: dx2 in /in/Aacol on line 20 Notice: Undefined variable: dx2 in /in/Aacol on line 20 Notice: Undefined variable: dy2 in /in/Aacol on line 20 Notice: Undefined variable: dy2 in /in/Aacol on line 20 Point 3 is closer to Point 1
Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.3
Notice: Undefined variable: dx2 in /in/Aacol on line 20 Notice: Undefined variable: dx2 in /in/Aacol on line 20 Notice: Undefined variable: dy2 in /in/Aacol on line 20 Notice: Undefined variable: dy2 in /in/Aacol on line 20 Point 3 is closer to Point 1

preferences:
68.46 ms | 2827 KiB | 4 Q