$html = <<<EOF
{$htmls}
EOF;
function randColor (){
$colors = array();
for($i = 0;$i<6;$i++){
$colors[] = dechex(rand(0,15));
}
return '#'.implode('',$colors);
};
function htmls($a,$b)
{
echo '<table>';
for ($i=1; $i<=$a; $i++) {
echo '<tr>';
for ($j=1; $j<=$b; $j++) {
echo '<td style="height: 50px; width: 50px; background-color:'.randColor().'">1</td>';
}
echo '</tr>';
}
echo '</table>';
}
$htmls = htmls(3,3);
echo $html;
2.用php实现具有简单功能的计算器
// 通过POST取值
$left = $_POST['left'] ?? 0;
$right = $_POST['right'] ?? 0;
$operator = $_POST['operator'];
// 计算函数
function total($a = 0, $b = 0, $opt = '+')
{
switch ($opt):
case '+':
return $a + $b;
case '-':
return $a - $b;
case '*':
return $a * $b;
case '/':
return $a / $b;
endswitch;
}
// 调用函数并赋值
$total = total($left, $right, $operator) ?? 0;
?>
<form method="post" action="cales.php">
<input type="text" name="left" value="<?php echo $left ?>">
<select name="operator" id="">
<option value="+" <?php if ($operator == '+'): echo "selected"; endif ?>>+</option>
<option value="-" <?php if ($operator == '-'): echo "selected"; endif ?>>-</option>
<option value="*" <?php if ($operator == '*'): echo "selected"; endif ?>>*</option>
<option value="/" <?php if ($operator == '/'): echo "selected"; endif ?>>/</option>
</select>
<input type="text" name="right" value="<?php echo $right ?>">
<button type="submit" ">计算</button>
<input type="text" name="total" value="<?php echo $total ?>">
</form>