3v4l.org

run code in 300+ PHP versions simultaneously
<?php $iter = 0; function weight($array) { global $iter; $sum = array_sum($array); $rnd = mt_rand(0,$sum); foreach($array as $k=>$v) { $iter++; if($rnd <= $v) { return $k; } $rnd = $rnd - $v; } } $weights = [100, 200, 1000]; $values = array_fill(0, count($weights), 0); for ($x = 0; $x < 1000; $x++) { $w = weight($weights); $values[$w] = $values[$w] + 1; } echo "$iter iterations performed"; var_dump($values);
Output for 7.2.0
2694 iterations performedarray(3) { [0]=> int(70) [1]=> int(166) [2]=> int(764) }
Output for 7.1.7
2670 iterations performedarray(3) { [0]=> int(77) [1]=> int(176) [2]=> int(747) }
Output for 7.1.6
2693 iterations performedarray(3) { [0]=> int(79) [1]=> int(149) [2]=> int(772) }
Output for 7.1.5
2662 iterations performedarray(3) { [0]=> int(74) [1]=> int(190) [2]=> int(736) }
Output for 7.1.0
2685 iterations performedarray(3) { [0]=> int(82) [1]=> int(151) [2]=> int(767) }
Output for 7.0.20
2713 iterations performedarray(3) { [0]=> int(65) [1]=> int(157) [2]=> int(778) }
Output for 7.0.14
2674 iterations performedarray(3) { [0]=> int(89) [1]=> int(148) [2]=> int(763) }
Output for 7.0.10
2694 iterations performedarray(3) { [0]=> int(76) [1]=> int(154) [2]=> int(770) }
Output for 7.0.9
2671 iterations performedarray(3) { [0]=> int(80) [1]=> int(169) [2]=> int(751) }
Output for 7.0.8
2678 iterations performedarray(3) { [0]=> int(89) [1]=> int(144) [2]=> int(767) }
Output for 7.0.7
2686 iterations performedarray(3) { [0]=> int(78) [1]=> int(158) [2]=> int(764) }
Output for 7.0.6
2725 iterations performedarray(3) { [0]=> int(66) [1]=> int(143) [2]=> int(791) }
Output for 7.0.5
2695 iterations performedarray(3) { [0]=> int(74) [1]=> int(157) [2]=> int(769) }
Output for 7.0.4
2665 iterations performedarray(3) { [0]=> int(89) [1]=> int(157) [2]=> int(754) }
Output for 7.0.3
2710 iterations performedarray(3) { [0]=> int(65) [1]=> int(160) [2]=> int(775) }
Output for 7.0.2
2716 iterations performedarray(3) { [0]=> int(70) [1]=> int(144) [2]=> int(786) }
Output for 7.0.1
2727 iterations performedarray(3) { [0]=> int(69) [1]=> int(135) [2]=> int(796) }
Output for 7.0.0
2713 iterations performedarray(3) { [0]=> int(70) [1]=> int(147) [2]=> int(783) }
Output for 5.6.28
2681 iterations performedarray(3) { [0]=> int(79) [1]=> int(161) [2]=> int(760) }
Output for 5.6.25
2684 iterations performedarray(3) { [0]=> int(70) [1]=> int(176) [2]=> int(754) }
Output for 5.6.24
2700 iterations performedarray(3) { [0]=> int(71) [1]=> int(158) [2]=> int(771) }
Output for 5.6.23
2697 iterations performedarray(3) { [0]=> int(79) [1]=> int(145) [2]=> int(776) }
Output for 5.6.22
2705 iterations performedarray(3) { [0]=> int(72) [1]=> int(151) [2]=> int(777) }
Output for 5.6.21
2661 iterations performedarray(3) { [0]=> int(91) [1]=> int(157) [2]=> int(752) }
Output for 5.6.20
2696 iterations performedarray(3) { [0]=> int(75) [1]=> int(154) [2]=> int(771) }
Output for 5.6.19
2691 iterations performedarray(3) { [0]=> int(81) [1]=> int(147) [2]=> int(772) }
Output for 5.6.18
2682 iterations performedarray(3) { [0]=> int(73) [1]=> int(172) [2]=> int(755) }
Output for 5.6.17
2710 iterations performedarray(3) { [0]=> int(81) [1]=> int(128) [2]=> int(791) }
Output for 5.6.16
2670 iterations performedarray(3) { [0]=> int(87) [1]=> int(156) [2]=> int(757) }
Output for 5.6.15
2697 iterations performedarray(3) { [0]=> int(74) [1]=> int(155) [2]=> int(771) }
Output for 5.6.14
2696 iterations performedarray(3) { [0]=> int(73) [1]=> int(158) [2]=> int(769) }
Output for 5.6.13
2662 iterations performedarray(3) { [0]=> int(98) [1]=> int(142) [2]=> int(760) }
Output for 5.6.12
2695 iterations performedarray(3) { [0]=> int(76) [1]=> int(153) [2]=> int(771) }
Output for 5.6.11
2692 iterations performedarray(3) { [0]=> int(77) [1]=> int(154) [2]=> int(769) }
Output for 5.6.10
2714 iterations performedarray(3) { [0]=> int(72) [1]=> int(142) [2]=> int(786) }
Output for 5.6.9
2710 iterations performedarray(3) { [0]=> int(64) [1]=> int(162) [2]=> int(774) }
Output for 5.6.8
2677 iterations performedarray(3) { [0]=> int(78) [1]=> int(167) [2]=> int(755) }
Output for 5.5.34, 5.6.7
2679 iterations performedarray(3) { [0]=> int(81) [1]=> int(159) [2]=> int(760) }
Output for 5.6.6
2712 iterations performedarray(3) { [0]=> int(60) [1]=> int(168) [2]=> int(772) }
Output for 5.6.5
2688 iterations performedarray(3) { [0]=> int(79) [1]=> int(154) [2]=> int(767) }
Output for 5.6.4
2677 iterations performedarray(3) { [0]=> int(71) [1]=> int(181) [2]=> int(748) }
Output for 5.6.3
2712 iterations performedarray(3) { [0]=> int(75) [1]=> int(138) [2]=> int(787) }
Output for 5.6.2
2703 iterations performedarray(3) { [0]=> int(74) [1]=> int(149) [2]=> int(777) }
Output for 5.6.1
2696 iterations performedarray(3) { [0]=> int(78) [1]=> int(148) [2]=> int(774) }
Output for 5.6.0
2684 iterations performedarray(3) { [0]=> int(73) [1]=> int(170) [2]=> int(757) }
Output for 5.5.38
2703 iterations performedarray(3) { [0]=> int(73) [1]=> int(151) [2]=> int(776) }
Output for 5.5.37
2664 iterations performedarray(3) { [0]=> int(85) [1]=> int(166) [2]=> int(749) }
Output for 5.5.36
2711 iterations performedarray(3) { [0]=> int(70) [1]=> int(149) [2]=> int(781) }
Output for 5.5.35
2698 iterations performedarray(3) { [0]=> int(68) [1]=> int(166) [2]=> int(766) }
Output for 5.5.33
2683 iterations performedarray(3) { [0]=> int(81) [1]=> int(155) [2]=> int(764) }
Output for 5.5.32
2702 iterations performedarray(3) { [0]=> int(66) [1]=> int(166) [2]=> int(768) }
Output for 5.5.31
2671 iterations performedarray(3) { [0]=> int(95) [1]=> int(139) [2]=> int(766) }
Output for 5.5.30
2713 iterations performedarray(3) { [0]=> int(69) [1]=> int(149) [2]=> int(782) }
Output for 5.5.29
2701 iterations performedarray(3) { [0]=> int(68) [1]=> int(163) [2]=> int(769) }
Output for 5.5.28
2721 iterations performedarray(3) { [0]=> int(64) [1]=> int(151) [2]=> int(785) }
Output for 5.5.27
2679 iterations performedarray(3) { [0]=> int(79) [1]=> int(163) [2]=> int(758) }
Output for 5.4.39, 5.5.26
2708 iterations performedarray(3) { [0]=> int(77) [1]=> int(138) [2]=> int(785) }
Output for 5.5.25
2700 iterations performedarray(3) { [0]=> int(84) [1]=> int(132) [2]=> int(784) }
Output for 5.5.24
2686 iterations performedarray(3) { [0]=> int(76) [1]=> int(162) [2]=> int(762) }
Output for 5.5.23
2689 iterations performedarray(3) { [0]=> int(73) [1]=> int(165) [2]=> int(762) }
Output for 5.5.22
2704 iterations performedarray(3) { [0]=> int(71) [1]=> int(154) [2]=> int(775) }
Output for 5.5.21
2706 iterations performedarray(3) { [0]=> int(79) [1]=> int(136) [2]=> int(785) }
Output for 5.5.20
2702 iterations performedarray(3) { [0]=> int(77) [1]=> int(144) [2]=> int(779) }
Output for 5.5.19
2721 iterations performedarray(3) { [0]=> int(73) [1]=> int(133) [2]=> int(794) }
Output for 5.5.18
2692 iterations performedarray(3) { [0]=> int(65) [1]=> int(178) [2]=> int(757) }
Output for 5.5.16
2690 iterations performedarray(3) { [0]=> int(74) [1]=> int(162) [2]=> int(764) }
Output for 5.5.15
2699 iterations performedarray(3) { [0]=> int(73) [1]=> int(155) [2]=> int(772) }
Output for 5.5.14
2682 iterations performedarray(3) { [0]=> int(76) [1]=> int(166) [2]=> int(758) }
Output for 5.5.13
2682 iterations performedarray(3) { [0]=> int(84) [1]=> int(150) [2]=> int(766) }
Output for 5.5.12
2705 iterations performedarray(3) { [0]=> int(70) [1]=> int(155) [2]=> int(775) }
Output for 5.5.11
2706 iterations performedarray(3) { [0]=> int(76) [1]=> int(142) [2]=> int(782) }
Output for 5.5.10
2682 iterations performedarray(3) { [0]=> int(72) [1]=> int(174) [2]=> int(754) }
Output for 5.5.9
2703 iterations performedarray(3) { [0]=> int(75) [1]=> int(147) [2]=> int(778) }
Output for 5.5.8
2704 iterations performedarray(3) { [0]=> int(73) [1]=> int(150) [2]=> int(777) }
Output for 5.5.7
2659 iterations performedarray(3) { [0]=> int(89) [1]=> int(163) [2]=> int(748) }
Output for 5.5.6
2708 iterations performedarray(3) { [0]=> int(65) [1]=> int(162) [2]=> int(773) }
Output for 5.5.5
2739 iterations performedarray(3) { [0]=> int(62) [1]=> int(137) [2]=> int(801) }
Output for 5.5.4
2710 iterations performedarray(3) { [0]=> int(73) [1]=> int(144) [2]=> int(783) }
Output for 5.5.3
2736 iterations performedarray(3) { [0]=> int(69) [1]=> int(126) [2]=> int(805) }
Output for 5.5.2
2661 iterations performedarray(3) { [0]=> int(89) [1]=> int(161) [2]=> int(750) }
Output for 5.5.1
2688 iterations performedarray(3) { [0]=> int(77) [1]=> int(158) [2]=> int(765) }
Output for 5.5.0
2689 iterations performedarray(3) { [0]=> int(86) [1]=> int(139) [2]=> int(775) }
Output for 5.4.45
2702 iterations performedarray(3) { [0]=> int(75) [1]=> int(148) [2]=> int(777) }
Output for 5.4.44
2690 iterations performedarray(3) { [0]=> int(70) [1]=> int(170) [2]=> int(760) }
Output for 5.4.43
2666 iterations performedarray(3) { [0]=> int(89) [1]=> int(156) [2]=> int(755) }
Output for 5.4.42
2662 iterations performedarray(3) { [0]=> int(91) [1]=> int(156) [2]=> int(753) }
Output for 5.4.41
2721 iterations performedarray(3) { [0]=> int(69) [1]=> int(141) [2]=> int(790) }
Output for 5.4.40
2677 iterations performedarray(3) { [0]=> int(80) [1]=> int(163) [2]=> int(757) }
Output for 5.4.38
2715 iterations performedarray(3) { [0]=> int(68) [1]=> int(149) [2]=> int(783) }
Output for 5.4.37
2660 iterations performedarray(3) { [0]=> int(83) [1]=> int(174) [2]=> int(743) }
Output for 5.4.36
2688 iterations performedarray(3) { [0]=> int(85) [1]=> int(142) [2]=> int(773) }
Output for 5.4.35
2708 iterations performedarray(3) { [0]=> int(68) [1]=> int(156) [2]=> int(776) }
Output for 5.4.34
2716 iterations performedarray(3) { [0]=> int(73) [1]=> int(138) [2]=> int(789) }
Output for 5.4.32
2673 iterations performedarray(3) { [0]=> int(88) [1]=> int(151) [2]=> int(761) }
Output for 5.4.31
2654 iterations performedarray(3) { [0]=> int(98) [1]=> int(150) [2]=> int(752) }
Output for 5.4.30
2713 iterations performedarray(3) { [0]=> int(67) [1]=> int(153) [2]=> int(780) }
Output for 5.4.29
2674 iterations performedarray(3) { [0]=> int(92) [1]=> int(142) [2]=> int(766) }
Output for 5.4.28
2671 iterations performedarray(3) { [0]=> int(84) [1]=> int(161) [2]=> int(755) }
Output for 5.4.27
2663 iterations performedarray(3) { [0]=> int(85) [1]=> int(167) [2]=> int(748) }
Output for 5.4.26
2690 iterations performedarray(3) { [0]=> int(81) [1]=> int(148) [2]=> int(771) }
Output for 5.4.25
2631 iterations performedarray(3) { [0]=> int(106) [1]=> int(157) [2]=> int(737) }
Output for 5.4.24
2707 iterations performedarray(3) { [0]=> int(71) [1]=> int(151) [2]=> int(778) }
Output for 5.4.23
2703 iterations performedarray(3) { [0]=> int(80) [1]=> int(137) [2]=> int(783) }
Output for 5.4.22
2692 iterations performedarray(3) { [0]=> int(75) [1]=> int(158) [2]=> int(767) }
Output for 5.4.21
2651 iterations performedarray(3) { [0]=> int(96) [1]=> int(157) [2]=> int(747) }
Output for 5.4.20
2668 iterations performedarray(3) { [0]=> int(91) [1]=> int(150) [2]=> int(759) }
Output for 5.4.19
2695 iterations performedarray(3) { [0]=> int(84) [1]=> int(137) [2]=> int(779) }
Output for 5.4.18
2720 iterations performedarray(3) { [0]=> int(75) [1]=> int(130) [2]=> int(795) }
Output for 5.4.17
2686 iterations performedarray(3) { [0]=> int(81) [1]=> int(152) [2]=> int(767) }
Output for 5.4.16
2705 iterations performedarray(3) { [0]=> int(66) [1]=> int(163) [2]=> int(771) }
Output for 5.4.15
2689 iterations performedarray(3) { [0]=> int(72) [1]=> int(167) [2]=> int(761) }
Output for 5.4.14
2683 iterations performedarray(3) { [0]=> int(86) [1]=> int(145) [2]=> int(769) }
Output for 5.4.13
2692 iterations performedarray(3) { [0]=> int(74) [1]=> int(160) [2]=> int(766) }
Output for 5.4.12
2678 iterations performedarray(3) { [0]=> int(82) [1]=> int(158) [2]=> int(760) }
Output for 5.4.11
2670 iterations performedarray(3) { [0]=> int(74) [1]=> int(182) [2]=> int(744) }
Output for 5.4.10
2691 iterations performedarray(3) { [0]=> int(79) [1]=> int(151) [2]=> int(770) }
Output for 5.4.9
2681 iterations performedarray(3) { [0]=> int(91) [1]=> int(137) [2]=> int(772) }
Output for 5.4.8
2666 iterations performedarray(3) { [0]=> int(87) [1]=> int(160) [2]=> int(753) }
Output for 5.4.7
2715 iterations performedarray(3) { [0]=> int(61) [1]=> int(163) [2]=> int(776) }
Output for 5.4.6
2702 iterations performedarray(3) { [0]=> int(73) [1]=> int(152) [2]=> int(775) }
Output for 5.4.5
2730 iterations performedarray(3) { [0]=> int(69) [1]=> int(132) [2]=> int(799) }
Output for 5.4.4
2702 iterations performedarray(3) { [0]=> int(79) [1]=> int(140) [2]=> int(781) }
Output for 5.4.3
2742 iterations performedarray(3) { [0]=> int(49) [1]=> int(160) [2]=> int(791) }
Output for 5.4.2
2701 iterations performedarray(3) { [0]=> int(76) [1]=> int(147) [2]=> int(777) }
Output for 5.4.1
2689 iterations performedarray(3) { [0]=> int(70) [1]=> int(171) [2]=> int(759) }
Output for 5.4.0
2701 iterations performedarray(3) { [0]=> int(80) [1]=> int(139) [2]=> int(781) }
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/pcHDK on line 20
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/pcHDK on line 20
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/pcHDK on line 20
Process exited with code 255.

preferences:
149.67 ms | 401 KiB | 216 Q