Blogger Information
Blog 34
fans 0
comment 0
visits 18405
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0427PHP编程作业
千山暮雪
Original
569 people have browsed it

1.使用定界符heredoc输出九格宫表格,要求颜色各异,每个小格子宽高50px

  1. $html = <<<EOF
  2. {$htmls}
  3. EOF;
  4. function randColor (){
  5. $colors = array();
  6. for($i = 0;$i<6;$i++){
  7. $colors[] = dechex(rand(0,15));
  8. }
  9. return '#'.implode('',$colors);
  10. };
  11. function htmls($a,$b)
  12. {
  13. echo '<table>';
  14. for ($i=1; $i<=$a; $i++) {
  15. echo '<tr>';
  16. for ($j=1; $j<=$b; $j++) {
  17. echo '<td style="height: 50px; width: 50px; background-color:'.randColor().'">1</td>';
  18. }
  19. echo '</tr>';
  20. }
  21. echo '</table>';
  22. }
  23. $htmls = htmls(3,3);
  24. echo $html;

2.用php实现具有简单功能的计算器

  1. // 通过POST取值
  2. $left = $_POST['left'] ?? 0;
  3. $right = $_POST['right'] ?? 0;
  4. $operator = $_POST['operator'];
  5. // 计算函数
  6. function total($a = 0, $b = 0, $opt = '+')
  7. {
  8. switch ($opt):
  9. case '+':
  10. return $a + $b;
  11. case '-':
  12. return $a - $b;
  13. case '*':
  14. return $a * $b;
  15. case '/':
  16. return $a / $b;
  17. endswitch;
  18. }
  19. // 调用函数并赋值
  20. $total = total($left, $right, $operator) ?? 0;
  21. ?>
  22. <form method="post" action="cales.php">
  23. <input type="text" name="left" value="<?php echo $left ?>">
  24. <select name="operator" id="">
  25. <option value="+" <?php if ($operator == '+'): echo "selected"; endif ?>>+</option>
  26. <option value="-" <?php if ($operator == '-'): echo "selected"; endif ?>>-</option>
  27. <option value="*" <?php if ($operator == '*'): echo "selected"; endif ?>>*</option>
  28. <option value="/" <?php if ($operator == '/'): echo "selected"; endif ?>>/</option>
  29. </select>
  30. <input type="text" name="right" value="<?php echo $right ?>">
  31. <button type="submit" ">计算</button>
  32. <input type="text" name="total" value="<?php echo $total ?>">
  33. </form>
Correcting teacher:灭绝师太灭绝师太

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post