From 932ca10fda2b0e39e483195a34ad825fde90fd21 Mon Sep 17 00:00:00 2001 From: kuangweijian <867034892@qq.com> Date: Wed, 9 Mar 2022 08:37:02 +0800 Subject: [PATCH] =?UTF-8?q?php=E5=AD=A6=E4=B9=A0=EF=BC=9A=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E3=80=81=E5=AD=97=E7=AC=A6=E4=B8=B2=E3=80=81=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E3=80=81=E8=AE=BE=E7=BD=AE=E6=97=A5=E6=9C=9F=E6=97=B6?= =?UTF-8?q?=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 03函数/04匿名函数.php | 47 +++++++++++++++++ 03函数/05回调函数.php | 68 +++++++++++++++++++++++++ 03函数/06可变函数.php | 24 +++++++++ 03函数/07递归函数.php | 11 ++++ 04字符串/00字符串操作.php | 34 +++++++++++++ 04字符串/01单双引号.php | 10 ++++ 04字符串/02字符串拼接.php | 11 ++++ 04字符串/03字符串查找.php | 3 ++ 04字符串/04截取字符串.php | 11 ++++ 05数组/01数组.php | 64 +++++++++++++++++++++++ 05数组/02冒泡排序.php | 20 ++++++++ 06设置时间日期/01设置时区.php | 17 +++++++ 07面向对象/01.php | 20 ++++++++ 13 files changed, 340 insertions(+) create mode 100644 03函数/04匿名函数.php create mode 100644 03函数/05回调函数.php create mode 100644 03函数/06可变函数.php create mode 100644 03函数/07递归函数.php create mode 100644 04字符串/00字符串操作.php create mode 100644 04字符串/01单双引号.php create mode 100644 04字符串/02字符串拼接.php create mode 100644 04字符串/03字符串查找.php create mode 100644 04字符串/04截取字符串.php create mode 100644 05数组/01数组.php create mode 100644 05数组/02冒泡排序.php create mode 100644 06设置时间日期/01设置时区.php create mode 100644 07面向对象/01.php diff --git a/03函数/04匿名函数.php b/03函数/04匿名函数.php new file mode 100644 index 0000000..1cd9f9c --- /dev/null +++ b/03函数/04匿名函数.php @@ -0,0 +1,47 @@ +'; + // return 'hello'; + }; + + $a(); + + //查看变量内容 + var_dump(($a)); + + + //闭包=匿名函数 + //函数里面的函数 + + function func(){ + //定义局部变量 + $b='world'; + + $innerFunc = function(){ + //函数内部的函数 + + //调用函数func的局部变量 + echo $b.'
'; + }; + + //调用匿名函数 + $innerFunc(); + + } + + + + func(); + + // echo $a().'
'; + + // //匿名函数作为参数,回调函数 + // $arr=[1,2,3,4]; + // $ret=array_map(function ($num){ + // return $num*$num; + // }, $arr); + + // echo '
';
+    // print_r($ret);
+?>
\ No newline at end of file
diff --git a/03函数/05回调函数.php b/03函数/05回调函数.php
new file mode 100644
index 0000000..cef453a
--- /dev/null
+++ b/03函数/05回调函数.php
@@ -0,0 +1,68 @@
+';
+
+        return $opt_name($m,$n);
+    }
+
+    function add($m,$n)//回调函数
+    {
+        return $m+$n;
+    }
+
+    function subtraction($m,$n)
+    {
+        return $m-$n;
+    }
+
+    $ret=math('add',1,2);
+    echo $ret.'
'; + + $ret=math('subtraction',6,4); + echo $ret.'
'; + + /* + * add是回调函数,add作为参数传入math()中, + * 这样就可以把add(),和subtraction()公有的部分一起放入math()中,例如 echo "this is a math opt '$opt_name'".'
'; + * 如果math作为库函数,我们是不能修改的。 + * 如果不适用回调函数,那么代码将会这样 + + function add($m,$n)//回调函数 + { + echo "this is a math opt 'add'".'
'; + return $m+$n; + } + + function subtraction($m,$n) + { + echo "this is a math opt 'subtraction'".'
'; + return $m-$n; + } + + $ret=add(1,2); + echo $ret.'
'; + + $ret=subtraction(6,4); + echo $ret.'
'; + */ + + + //call_user_func() + function func_a($funcName,$m,$n) + { + return call_user_func($funcName, $m, $n); + } + + $ret=func_a('add',10,5); + echo $ret.'
'; + + //call_user_func_array() + function func_b($funcName,$m,$n) + { + return call_user_func_array($funcName, array($m,$n)); //以数组的形式接收回调函数的参数 + } + + $ret=func_b('add',2,100); + echo $ret.'
'; +?> \ No newline at end of file diff --git a/03函数/06可变函数.php b/03函数/06可变函数.php new file mode 100644 index 0000000..314ea6f --- /dev/null +++ b/03函数/06可变函数.php @@ -0,0 +1,24 @@ +"; + } + + function func_b($str='') + { + echo $str.''; + } + + function func_c($string) + { + echo $string; + } + + $funcname='func_a'; + $funcname(); //变量()就是可变函数,会执行与funcname命名相同的函数执行; + + $funcname='func_b'; + $funcname('this is func_b
'); + + $funcname='func_c'; + $funcname('this is func_c
'); +?> \ No newline at end of file diff --git a/03函数/07递归函数.php b/03函数/07递归函数.php new file mode 100644 index 0000000..43cef9a --- /dev/null +++ b/03函数/07递归函数.php @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/04字符串/00字符串操作.php b/04字符串/00字符串操作.php new file mode 100644 index 0000000..7679779 --- /dev/null +++ b/04字符串/00字符串操作.php @@ -0,0 +1,34 @@ +'; + + //获取字符串长度 + $str='hello world'; + echo $str.' len:'.strlen($str).'
'; + + //获取中文字符串的长度 + $str='你好世界'; + $len=mb_strlen($str); + echo $str.' len:'.$len.'
'; + + //自动转义字符串,常用于数据库 + $sql="select * from talbe1 where 'name'='邝伟键'"; + $str=addslashes($sql); + echo $str.'
'; // select * from talbe1 where \'name\'=\'邝伟键\' + + //还原转义字符 + $str=stripslashes($str); + echo $str.'
'; // select * from talbe1 where 'name'='邝伟键' + + //分割字符串并转为数组 + $str='hello,world,php'; + $arr=explode(',',$str); + echo '
';
+    print_r($arr);
+    echo '
'; + +?> \ No newline at end of file diff --git a/04字符串/01单双引号.php b/04字符串/01单双引号.php new file mode 100644 index 0000000..49fe14f --- /dev/null +++ b/04字符串/01单双引号.php @@ -0,0 +1,10 @@ +"; + $str2="{$a}是你好
"; + + echo $str1; + echo $str2; +?> \ No newline at end of file diff --git a/04字符串/02字符串拼接.php b/04字符串/02字符串拼接.php new file mode 100644 index 0000000..e9c8566 --- /dev/null +++ b/04字符串/02字符串拼接.php @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/04字符串/03字符串查找.php b/04字符串/03字符串查找.php new file mode 100644 index 0000000..69007d5 --- /dev/null +++ b/04字符串/03字符串查找.php @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/04字符串/04截取字符串.php b/04字符串/04截取字符串.php new file mode 100644 index 0000000..ef158fe --- /dev/null +++ b/04字符串/04截取字符串.php @@ -0,0 +1,11 @@ +'; + + //截取中文字符 + $str='一二三四五六七八九十'; + $ret=mb_substr($str, 2, 4); + echo $ret.'
'; +?> \ No newline at end of file diff --git a/05数组/01数组.php b/05数组/01数组.php new file mode 100644 index 0000000..ff31f35 --- /dev/null +++ b/05数组/01数组.php @@ -0,0 +1,64 @@ +'; + var_dump($arr); + + //定义二维数组 + $arr = array( + array('name'=>'邝','chinese'=>'90'), + array('name'=>'伟','chinese'=>'65') + ); + echo '
';
+    var_dump($arr);
+    echo $arr[0]['name'].'的分数是'.$arr[0]['chinese'];
+
+    echo '
'; + + //统计数组元素数量 + $arr = array('a', 'b','c','hello'); + $len = count($arr); + $len2 = sizeof($arr); + echo '$arr'.'的长度是'.$len.'
'; + echo '$arr'.'的长度是'.$len2.'
'; + + //返回放弃数组指针指向的元素 + $arr = array('a', 'b','c','hello'); + $curVal = current($arr); + echo '当前的元素是'.$curVal.'
'; + + //获取当前的元素的键名 + $arr=array('name'=>'邝','chinese'=>'90','city'=>'东莞'); + $curKey = key($arr); + echo '当前的键名是'.$curKey.'
'; + + //判断数组的键名或索引是否存在 + $arr=array('name'=>'邝','chinese'=>'90','city'=>'东莞'); + if (array_key_exists('chinese',$arr)) { + echo '键名存在'.'
'; + }else{ + echo '键名不存在'.'
'; + } + + + //判断数组中某个值是否存在 + $arr=array('name'=>'邝','chinese'=>'90','city'=>'东莞'); + //判断'东莞'是否存在 + $needle='东莞'; + $ret=in_array($needle,$arr,true); + if ($ret==TRUE) { + echo "数组中包含$needle".'
'; + }else{ + echo "数组中不包含$needle".'
'; + } + + //数组合成一个字符串 + $arr=array('name'=>'邝','chinese'=>'90','city'=>'东莞'); + $str=implode($arr); + echo $str.'
'; + + //把数组中的值赋给一组变量 + $arr=array('邝','90','东莞'); + list($a,$b,$c) = $arr; + echo '$a = '.$a.'
'.'$b = '.$b.'
'.'$c = '.$c.'
'; +?> \ No newline at end of file diff --git a/05数组/02冒泡排序.php b/05数组/02冒泡排序.php new file mode 100644 index 0000000..5b95995 --- /dev/null +++ b/05数组/02冒泡排序.php @@ -0,0 +1,20 @@ + $arr[$i+1]) + { + $tmp = $arr[$i+1]; + $arr[$i+1] = $arr[$i]; + $arr[$i] = $tmp; + } + } + + } + + echo '
';
+    print_r($arr);
+?>
\ No newline at end of file
diff --git a/06设置时间日期/01设置时区.php b/06设置时间日期/01设置时区.php
new file mode 100644
index 0000000..618026b
--- /dev/null
+++ b/06设置时间日期/01设置时区.php
@@ -0,0 +1,17 @@
+';
+    var_dump(getdate());
+
+    //检查日期时间是否有效
+    $ret=checkdate(3,32,2022);
+    if ($ret==TRUE) {
+        echo '日期有效'.'
'; + }else{ + echo '日期无效'.'
'; + } +?> \ No newline at end of file diff --git a/07面向对象/01.php b/07面向对象/01.php new file mode 100644 index 0000000..7f2c6ce --- /dev/null +++ b/07面向对象/01.php @@ -0,0 +1,20 @@ + \ No newline at end of file