Blogger Information
Blog 13
fans 0
comment 0
visits 9817
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
运算符 赋值运算符 字符串函数及自定义函数
樱风醉
Original
794 people have browsed it

一.运算符

  1. <?php
  2. //运算符
  3. $x = 88;
  4. $y = 66;
  5. //加
  6. echo $x + $y;
  7. echo '<hr>';
  8. //减
  9. echo $x - $y;
  10. echo '<hr>';
  11. //乘
  12. echo $x * $y;
  13. echo '<hr>';
  14. //除
  15. echo $x / $y;
  16. echo '<hr>';
  17. //取余
  18. echo $x % $y;
  19. echo '<hr>';
  20. //连接
  21. echo $x.'+'.$y.'='.$x+$y;
  22. echo '<hr>';
  23. //自增
  24. $a = 99;$b = 99;
  25. echo $a++;
  26. echo '<br>';
  27. echo $a;
  28. echo '<br>';
  29. echo ++$b;
  30. echo '<br>';
  31. echo $b;
  32. echo '<hr>';
  33. //自减
  34. $c = 99;$d = 99;
  35. echo $c--;
  36. echo '<br>';
  37. echo $c;
  38. echo '<br>';
  39. echo --$d;
  40. echo '<br>';
  41. echo $d;
  42. echo '<hr>';
  43. ?>

结果图:

二.赋值运算符

  1. <?php
  2. //赋值运算符
  3. //赋值,左边等于右边,x=y
  4. $a = 100;
  5. echo $a;
  6. echo '<hr>';
  7. //加等,x+=y>>>x=x+y
  8. $b = 1;
  9. echo $b += 99;
  10. echo '<hr>';
  11. //减等,x-=y>>>x=x-y
  12. $c = 199;
  13. echo $c -= 99;
  14. echo '<hr>';
  15. //乘等,x*=y>>>x=x*y
  16. $d = 10;
  17. echo $d *= 10;
  18. echo '<hr>';
  19. //除等,x/=y>>>x=x/y
  20. $e = 1000;
  21. echo $e /= 10;
  22. echo '<hr>';
  23. //取余等于,x%=y>>>x=x%y
  24. $f = 100;
  25. echo $f %= 4;
  26. echo '<hr>';
  27. //连接
  28. $g="Hello";
  29. $g .= " world!";
  30. echo $g;
  31. ?>

结果图:

三.字符串函数

  1. <?php
  2. //字符串函数
  3. $nickName = 'shiro';
  4. //strtoupper()将字符串小写字母转为大写
  5. echo strtoupper($nickName);
  6. echo '<hr>';
  7. //strlen()获取字符串长度
  8. echo strlen($nickName);
  9. echo '<hr>';
  10. //strtolower()将字符串大写字母转化为小写
  11. $nickName = 'NEKO';
  12. echo strtolower($nickName);
  13. echo '<hr>';
  14. //trim()去掉字符串两边的空白
  15. echo ' study ';
  16. echo '<br>';
  17. echo trim(' study ');
  18. echo '<hr>';
  19. //strrev()反转字符串,只能反转英文
  20. echo strrev("Hello world!");
  21. echo '<hr>';
  22. //strpos()函数用于检索字符串内指定的字符或文本
  23. echo strpos("Hello world!",'w');
  24. echo '<hr>';
  25. //ucfirs()首字母大写
  26. echo ucfirst("program files");
  27. echo '<hr>';
  28. //ucwords()把字符串中每个单词的首字符转换为大写
  29. echo ucwords("program files");
  30. echo '<hr>';
  31. //str_repeat()把字符串重复指定的次数
  32. echo str_repeat("php",3);
  33. echo '<hr>';
  34. //str_replace() 替换字符串中的一些字符为指定字符(区分大小写)
  35. echo str_replace("World","php中文网","Hello World!");
  36. echo '<hr>';
  37. ?>

结果图:

四.自定义函数(动态表格)

  1. <?php
  2. $purchase = [
  3. [
  4. 'name' => '笔记本',
  5. 'price' => 5 ,
  6. 'number' => 200 ,
  7. ],
  8. [
  9. 'name' => '钢笔',
  10. 'price' => 5 ,
  11. 'number' => 200 ,
  12. ],
  13. [
  14. 'name' => '显示器',
  15. 'price' => 500 ,
  16. 'number' => 20 ,
  17. ],
  18. [
  19. 'name' => '电脑主机',
  20. 'price' => 6000 ,
  21. 'number' => 20 ,
  22. ],
  23. [
  24. 'name' => '键鼠套装',
  25. 'price' => 55 ,
  26. 'number' => 20 ,
  27. ],
  28. [
  29. 'name' => '桌子',
  30. 'price' => 550 ,
  31. 'number' => 25 ,
  32. ]
  33. ];
  34. $thead = [
  35. '序号',
  36. '名称',
  37. '数量',
  38. '单价',
  39. '金额'
  40. ];
  41. function table($purchase,$thead){
  42. $table = '<table>';
  43. $table .='<thead>'.'<tr>';
  44. foreach ($thead as $thead_k => $thead_v) {
  45. $table .= '<th>'.$thead_v.'</th>';
  46. }
  47. $table .= '</tr>'.'</thead>';
  48. $table .= '<tbody>';
  49. foreach ($purchase as $item_k => $items) {
  50. $table .= '<tr>';
  51. $table .= '<td>'.($item_k + 1).'</td>';
  52. // $table .= '<td>'.$items['name'].'</td>';
  53. // $table .= '<td>'.$items['price'].'</td>';
  54. // $table .= '<td>'.$items['number'].'</td>';
  55. // $table .= '<td>'.($items['price'] * $items['number']).'</td>';
  56. foreach ($items as $item) {
  57. $table .= '<td>'.$item.'</td>';
  58. }
  59. $table .= '<td>'.($items['price'] * $items['number']).'</td>';
  60. $table .= '<tr>';
  61. }
  62. $table .= '</tbody>';
  63. $table .= '</table>';
  64. return $table;
  65. }
  66. ?>
  67. <!DOCTYPE html>
  68. <html lang="zh-CN">
  69. <head>
  70. <meta charset="UTF-8">
  71. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  72. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  73. <title>采购单</title>
  74. <style>
  75. table{
  76. border: 1px solid black;
  77. border-collapse: collapse;
  78. }
  79. td,
  80. th {
  81. width: 120px;
  82. border: 1px solid black;
  83. text-align: center;
  84. }
  85. </style>
  86. </head>
  87. <body>
  88. <?php echo table($purchase ,$thead); ?>
  89. </body>
  90. </html>

结果图:

Correcting teacher:PHPzPHPz

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