Blogger Information
Blog 49
fans 0
comment 0
visits 38135
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
初识 php 数值运算符与字符串运算符、字符串常用函数与自定义函数
超超多喝水
Original
638 people have browsed it

初识 php 数值运算符与字符串运算符、字符串常用函数与自定义函数

php 数值运算符与字符串运算符

php 基础运算符有加、减、乘、除、取余、赋值运算符、字符串拼接运算符等

  1. <?php
  2. //赋值运算符 = :变量 = 值;
  3. $num = 66;
  4. $str = '666老铁';
  5. // 加+
  6. echo '加+:' . ($num + $num);
  7. echo '<hr>';
  8. //减-
  9. echo '减-:' . ($num - 33);
  10. echo '<hr>';
  11. //乘*
  12. echo '乘*:' . ($num * 2);
  13. echo '<hr>';
  14. //除/
  15. echo '除/:' . ($num / 2);
  16. echo '<hr>';
  17. //取余%
  18. echo '取余%:' . ($num % 4);
  19. echo '<hr>';
  20. //幂运算
  21. echo '幂运算:' . ($num ** 2);
  22. echo '<hr>';
  23. //字符串运算符.
  24. echo '字符串运算符:' . $str . $str;
  25. echo '<hr>';
  26. //如果字符串开头是数字也可以与数字相加,但是直接加会报错,可以把字符串转为数字类型
  27. echo '字符串与数字相加:' . ($num + (int)$str);
  28. ?>

运算符

php 字符串常用函数

  • “echo()”输出一个或多个字符串
  • “explode(“按照哪个字符串为节点分割”,”需要分割的字符串”,分割几个字符串(可选))”使用一个字符串分割另一个字符串为数组
  • “trim();ltrim();rtrim()”去除字符串中左右;左侧;右侧空格
  • “strlen()”获取字符串长度
  • “strtoupper()”将字符串所有字母大写
  • “strtolower()”将字符串所有字母小写
  • “substr_count(“被检测的字符串”,”查找哪个字符串”)”计算字符串出现的次数
  • “substr_replace(“被检测的字符串”,”需要替换成哪个字符串”,从哪个位置开始,替换多少个字符(可选))”替换字符串的子串
  • “substr(“被处理的字符串”,开始位置,截取长度(可选))”返回字符串的子串
  • “strip_tags()”去除字符串中所有的 html 代码及 php 代码
  • “htmlspecialchars()”将包含 html 的代码原样输出
  1. <?php
  2. echo "echo(\"string\"):" . "string";
  3. echo "<hr>";
  4. echo "explode(\",\", \"string1,string2,string3\"):";
  5. echo "<pre>";
  6. print_r(explode(",", "string1,string2,string3"));
  7. echo "<hr>";
  8. echo "trim(\" string \"):";
  9. echo trim(" string ");
  10. echo "<hr>";
  11. echo "ltrim(\" string \"):";
  12. echo ltrim(" string ");
  13. echo "<hr>";
  14. echo "rtrim(\" string \"):";
  15. echo rtrim(" string ");
  16. echo "<hr>";
  17. echo "strlen(\"string\"):";
  18. echo strlen("string");
  19. echo "<hr>";
  20. echo "strtoupper(\"StRing\"):";
  21. echo strtoupper("StRing");
  22. echo "<hr>";
  23. echo "strtolower(\"StRing\"):";
  24. echo strtolower("StRing");
  25. echo "<hr>";
  26. echo "substr_count(\"string1,string2,string3\", \"string\"):";
  27. echo substr_count("string1,string2,string3", "string");
  28. echo "<hr>";
  29. echo "substr_replace(\"string1,string2,string3\", \"str\", 8, 7):";
  30. echo substr_replace("string1,string2,string3", "str", 8, 7);
  31. echo "<hr>";
  32. echo "substr(\"string1,string2,string3\", 8, 7):";
  33. echo substr("string1,string2,string3", 8, 7);
  34. echo "<hr>";
  35. echo htmlspecialchars("strip_tags(<div>string</div>):");
  36. echo strip_tags("<div>string</div>");
  37. echo "<hr>";
  38. echo htmlspecialchars("htmlspecialchars(\"<div>string</div>\"):");
  39. echo htmlspecialchars("<div>string</div>");
  40. ?>

字符串

自定义函数

自定义函数需要以 function 关键字开头后面跟一个函数名,函数名的命名要求跟变量一致,后面跟小括号,小括号内可以加入一个或多个形参,再后面是大括号,大括号的代码块内加入封装的代码,函数代码块最后要用 return 将最后的内容返回,需要注意:代码块内 return 后面不要再加别的代码,因为 return 后面的代码将不再执行

  1. <?php
  2. //数据信息
  3. $arr = [
  4. [
  5. 'name' => 'admin01',
  6. 'age' => 18,
  7. 'gender' => '男',
  8. 'school' => "php middle school"
  9. ],
  10. [
  11. 'name' => 'admin02',
  12. 'age' => 20,
  13. 'gender' => '女',
  14. 'school' => "js middle school"
  15. ],
  16. [
  17. 'name' => 'admin03',
  18. 'age' => 22,
  19. 'gender' => '男',
  20. 'school' => "css middle school"
  21. ],
  22. [
  23. 'name' => 'admin04',
  24. 'age' => 24,
  25. 'gender' => '女',
  26. 'school' => "html middle school"
  27. ],
  28. [
  29. 'name' => 'admin05',
  30. 'age' => 26,
  31. 'gender' => '男',
  32. 'school' => "python middle school"
  33. ]
  34. ];
  35. //表头信息
  36. $head = [
  37. "姓名",
  38. "年龄",
  39. "性别",
  40. "所属学校"
  41. ];
  42. function teacherInfo($info, $hd)
  43. {
  44. //建立table标签
  45. $table = "<table style=\"border-collapse: collapse;background-color: lightgreen\">";
  46. $table .= "<caption style=\"font-weight:bolder\">教师信息表</caption>";
  47. $table .= "<thead><tr>";
  48. foreach ($hd as $val) {
  49. $table .= "<th style=\"border:1px solid;background-color: lightgray\">$val</th>";
  50. }
  51. $table .= "</tr></thead>";
  52. $table .= "<tbody>";
  53. foreach ($info as $per) {
  54. // echo"<pre>";
  55. // print_r($per);
  56. $table .= "<tr>";
  57. //常规写法
  58. // $table .= "<td style=\"border:1px solid\">{$per['name']}</td>";
  59. // $table .= "<td style=\"border:1px solid\">{$per['age']}</td>";
  60. // $table .= "<td style=\"border:1px solid\">{$per['gender']}</td>";
  61. // $table .= "<td style=\"border:1px solid\">{$per['school']}</td>";
  62. //如果内部顺序确定,可以进一步遍历简化
  63. foreach($per as $k=>$v){
  64. $table .= "<td style=\"border:1px solid\">$v</td>";
  65. }
  66. $table .= "</tr>";
  67. }
  68. $table .= "</tbody>";
  69. $table .= "</table>";
  70. return $table;
  71. }
  72. ?>
  73. <!DOCTYPE html>
  74. <html lang="zh-CN">
  75. <head>
  76. <meta charset="UTF-8">
  77. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  78. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  79. <title>教师信息表</title>
  80. </head>
  81. <body>
  82. <?php echo teacherInfo($arr, $head) ?>
  83. </body>
  84. </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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!