02c05-1.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 27 行

PHP
27
字号
<?php// Dice Simulatorfunction roll_dice($number, $sides, $values = false) {	// Loop as much as needed filling an array with the random die rolls.	$dice = array();	for ($i = 0; $i < $number; $i++) {		$dice[] = rand(1, $sides);	}		// If the optional 3rd parameter is set to true, return the dice array:	if ($values) {		return $dice;	} else {		// Otherwise, return the sum of the dice:		return array_sum($dice);	}}// Roll 3 six sided dice and echo their result (between 3 & 18)$roll_3d6 = roll_dice(3, 6);echo "<pre>Dice roll = {$roll_3d6}\n";// Roll a 100 sided die, 10 times, and echo all the values:$rm_stats = roll_dice(10, 100, true);print_r($rm_stats);echo '</pre>';?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?