PHP学习总结(函数、数组、字符串)
这次主要学习一下PHP中的函数、数组和字符串。
函数【1】-自定义函数
function 函数名([形式参数1,形式参数2,....形式参数n]){ //各种PHP代码.... //...... return 表达式;//也可以不返回,如果不写那么默认返回null }
函数的调用:
例如:
<?php function add($a,$b){ echo $a+$b; } add(10,30); ?>
输出结果:
40
函数的返回值:
返回给调用者,默认返回null
函数【2】-变量范围
1.局部变量
<?php function test(){ $a=1;//局部变量,只在这个函数内部有效 } echo $a; ?>
2.全局变量
注意:在PHP函数中无法直接使用全局变量,如需使用必须使用global声明变量.
<?php $i=10;//全局变量(外部变量) function test(){ global $i;//声明$i为全局变量,然后这个函数内部就可以使用 echo $i; } test(); ?>
3.定义一个常量
<?php define('MY_NAME','zzh'); function test(){ echo MY_NAME; } test(); ?>
输出结果:
zzh
4.静态变量
<?php function test(){ static $a=10; $a++; echo $a; } test(); echo '<br />'; test(); echo '<br />'; test(); echo '<br />'; ?>
1112 13 //值不会丢失 会保留原来存储的数据。
函数【3】-参数的传递
1.按值传递参数
2.按引用传递参数
例如:
<?php function test(&$a){ echo ++$a; } $i=10; test($i); echo '<br />'.$i; ?>
输出结果:
1111
3.函数的默认参数
例如:
<?php function test($a=10){ echo ++$a; } test(); ?>
输出结果:
11
4.可变长度参数列表
PHP提供,可直接使用
func_get_args(); func_get_arg(); func_num_args();
这三个函数可以使用在我们的自定义函数内部,能够返回给我们一些关于参数的信息!
例如:
<?php function test(){ var_dump(func_get_args());//作用是获取数变成数组 } test(1,2,3) ?>
输出结果:
array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }
例如:
<?php function test(){ var_dump(func_get_arg(0));//作用是提取数组中一个数 } test(1,2,3) ?>
输出结果:
int(1)
例如:
<?php function test(){ var_dump(func_num_args());//返回传入所在自定义函数的参数个数 } test(1,2,3) ?>
输出结果:
int(3)
5.可变函数(变量函数)
例如:
<?php function test(){ echo 'www!'; } $a='test';//把函数名字加上引号,赋值给变量名即可 $a();//相当于test(); ?>
输出结果:
www!
函数【4】-递归函数
递归函数即在函数内部自己调用自己的函数
<?php function test($n){ echo $n.' '; if($n>0){ test($n-1); } else{ echo '<-->'; } echo $n.' '; } test(3); ?>
输出结果:
3 2 1 0 <-->0 1 2 3
递归函数:
1)函数在被调用的执行过程中会在内存里面分配空间用于存储临时数据,那么函数在执行过程中默认之间时没有联系的(除了静态变量,按引用传,全局变量)。里面的变量默认都是局部变量,相互之间没有影响!
2)递归函数,在函数的内部需要有适时结束函数运行的条件!
函数【5】-内部(内置)函数
PHP提供给我们许多现成的函数或者结构,我们可以在实际的开发中直接使用!
数组【1】-数组与创建数组
一.数组类型
数组是一种复合数据类型
索引值: 就相当于给里面的每一个数据起个名字
1.索引数组
名字如果取得是整数:那么这个数组就是 索引数组
2.关联数组
名字如果取得是字符串类型: 那么在数组就是 关联数组
二.数组创建
第一种
<?php //$student[索引值]=具体的值 $student[0]=10; $student[1]='万维网'; $student[2]=true; $student[3]=11.1; //需要使用print_r()函数来输出数组的具体内容 print_r($student); var_dump($student);//也可以用var_dump来输出 ?>
输出结果:
Array ( [0] => 10 [1] => 万维网 [2] => 1 [3] => 11.1 )
第二种
<?php //如果省略索引值不写那么默认索引值就是整数,并且从0开始。 $student[]=10; $student[]='万维网'; $student[]=true; $student[]=11.1; var_dump($student); ?>
输出结果:
array(4) { [0]=> int(10) [1]=> string(9) "万维网" [2]=> bool(true) [3]=> float(11.1) }
第三种
<?php //两种方式方式输出都可以 //$student=array(索引值=>具体的值,......); //$student=array(10,'万维网',true,11.1); //var_dump($student); $student1=array( 0=>10, 1=>'万维网', 2=>true, 3=>11.1 ); var_dump($student1); ?>
输出结果:
array(4) { [0]=> int(10) [1]=> string(9) "万维网" [2]=> bool(true) [3]=> float(11.1) }array(4) { [0]=> int(10) [1]=> string(9) "万维网" [2]=> bool(true) [3]=> float(11.1) }
关联数组:
第一种
<?php $student['num']=10; $student['name']='万维网'; $student['sex']=true; $student['grade']=11.1; var_dump($student); //使用数组里面具体数据的方法 //数组变量名称[索引值]; //echo $student[1]; ?>
输出结果:
array(4) { ["num"]=> int(10) ["name"]=> string(9) "万维网" ["sex"]=> bool(true) ["grade"]=> float(11.1) }
第二种
<?php $student=array( 'num'=>11, 'name'=>'万维网', 'sex'=>true, 'grade'=>80.5 ); var_dump($student); ?>
输出结果:
array(4) { ["num"]=> int(11) ["name"]=> string(9) "万维网" ["sex"]=> bool(true) ["grade"]=> float(80.5) }
第三种
//二维数组,多维数组
<?php $student=arry( 0=>array(1,'万维网',true,60.5), 1=>array(2,'嗡嗡嗡',true,30.5), 2=>array(3,'哈哈哈',false,60.3) ); var_dump($student); ?>
输出结果:
源代码:
array(3) { [0]=> array(4) { [0]=> int(1)[1]=> string(9) "万维网" [2]=> bool(true) [3]=> float(60.5) } [1]=> array(4) { [0]=> int(2) [1]=> string(9) "嗡嗡嗡" [2]=> bool(true) [3]=> float(30.5) } [2]=> array(4) { [0]=> int(3)[1]=> string(9) "哈哈哈" [2]=> bool(false) [3]=> float(60.3) }
数组【2】-遍历函数
遍历数组:
1.for循环
例如:
<?php $arr=array( 'a','b','c','d','e','f' ); //count(数组)返回数组里面数据的个数,还可以获取多维数组的个数 //echo count($arr); $arr1=array( array(1,2,3), array(4,5,6) ); echo count($arr1); ?>
输出结果:
//6 2
也可以这样输出:
<?php $arr=array( 'a','b','c','d','e','f' ); for($i=0;$i<count($arr);$i++){ echo $arr[$i]; } ?>
输出结果:
abcdef
2.foreach语句来实现遍历数组
foreach(数组变量 as 变量1){ //每次循环执行的语句 变量1代表当前正在经历的数据 }
例如:
<?php //foreach用来遍历数组 $arr=array( 'name'=>'万维网', 'num'=>10 ); foreach($arr as $value){ echo $value.'<br />'; } ?>
输出结果:
万维网 10
foreach(数组变量 as 变量1=>变量2){ //每次循环执行的语句 变量1代表当前正在经历(访问)的数据的索引值 变量1代表当前正在经历(访问)的数据 }
预定义超全局数组变量
已经定义好了(存在)的变量(存放的数据的类型是数组)。
超全局:超级全局变量,作用域(有效区域)!
传递数据(提交数据)给服务器端主要两种方式
1.get方式
比如:
?参数名=参数值&参数名=参数值…
http://localhost/demo5_3/index.php?参数名=参数值
在服务器端(请求的php文件这边)可以通过$_GET来获取到
GET索引值为参数名,索引值对应的数据就是参数值通过vardump(_GET索引值为参数名,索引值对应的数据就是参数值
通过 var_dump(GET索引值为参数名,索引值对应的数据就是参数值通过vardump(GET)
2.post方式
通过var_dump(POST)比如表单post发送过来的!可以通过POST)比如表单 post发送过来的!可以通过POST)比如表单post发送过来的!可以通过_POST来获取到!
字符串处理
一、去除空白或其他字符函数
1.trim
去除空白 例如: <?php $str=' abc '; var_dump($str); var_dump(trim($str)); ?>
输出结果:
string(9) " abc " string(3) "abc"
去除其他字符函数 <?php $str='abcabcdefac'; var_dump($str); var_dump(trim($str,'bac')); ?>
输出结果:
string(11) "abcabcdefac" string(3) "def"
二、大小写转换函数
1.strtoupper
转化为大写 <?php $str='test'; var_dump($str); $str1=strtoupper($str); var_dump($str1); ?>
输出结果:
string(4) "test" string(4) "TEST"
2.strtolower
转化成小写 <?php $str='TeSt'; var_dump($str); var_dump(strtolower($str)); ?>
输出结果:
string(4) "TeSt" string(4) "test"
三、字符串查找函数
1.substr_count
计算字符串出现的次数 <?php $str='testteste'; var_dump(substr_count($str,'te')); ?>
输出结果:
int(3)
2.strpos
查找字符串首次出现的位置 <?php $str='testteste'; var_dump(strpos($str, 't1')); if(strpos($str, 't')===false){ echo '没找到!'; }else{ echo '找到啦!'; } ?>
输出结果:
bool(false) 找到啦!
3.strstr
查找字符串的首次出现 <?php $str='testteste'; var_dump($str); var_dump(strstr($str,'s')); var_dump(strstr($str,'s',true)); //true返回之前的部分 ?>
输出结果:
string(9) "testteste" string(7) "stteste" string(2) "te"
四、字符串替换函数
1.str_replace
字符串替换 <?php $str=array( 'abc123abc', '123abcabc', 'abcab123c' ); var_dump($str); $str1=str_replace(array('1','2','3'),array('一','二','三'),$str); //将1,2,3变成一,二,三 var_dump($str1); ?>
输出结果:
array(3) { [0]=> string(9) "abc123abc" [1]=> string(9) "123abcabc" [2]=> string(9) "abcab123c" } array(3) { [0]=> string(15) "abc一二三abc" [1]=> string(15) "一二三abcabc" [2]=> string(15) "abcab一二三c" }
五、与html标签相关的函数
1.htmlspecialchars
把预定义的字符转化为HTML实体 <?php $str = "This is some <b>bold</b> text."; echo htmlspecialchars($str); ?>
页面源代码
This is some <b>bold</b> text.
2.strip_tags
剥去字符串中的 HTML、XML 以及 PHP 的标签 <?php echo strip_tags("Hello <b>world!</b>"); ?>
页面源代码
Hello world!
六、字符串截取函数
1.substr
返回字符串的一部分 <?php $str='testdqwdwqdwqdqdwq'; echo substr($str,3,2); //第三次参数是截取的长度 ?>
输出结果:
td
$str='test'; echo substr($str,-3,2); //负数从右边数
输出结果:
es
七、字符串分隔函数
1.explode
字符串打散为数组 <?php $str='test,test1,test2,test3'; var_dump(explode(',',$str)); ?>
输出结果:
array(4) { [0]=> string(4) "test" [1]=> string(5) "test1" [2]=> string(5) "test2" [3]=> string(5) "test3" }
2.str_split
str_split() 函数把字符串分割到数组中 <?php $str='test'; var_dump(str_split($str)); ?>
输出结果:
array(4) { [0]=> string(1) "t" [1]=> string(1) "e" [2]=> string(1) "s" [3]=> string(1) "t" }
- 基于php常用函数总结(数组,字符串,时间,文件操作)
- 基于php常用函数总结(数组,字符串,时间,文件操作)
- 学习笔记PHP字符串处理函数总结
- PHP和js数组、字符串、函数不同点基础总结
- php 常用函数总结(数组,字符串,时间,文件操作)
- C语句基础学习(五、六)——数组和字符串、函数(总结于创智播客讲义)
- 基于php常用函数总结(数组,字符串,时间,文件操作)
- 实际开发中可能用到的PHP的字符串操作函数(总结)
- php学习笔记 数组的常用函数
- Php常用文件操作函数,正则表达式,个人学习总结
- php学习笔记(日期函数用法总结)
- PHP中的数组处理函数总结
- php数组查找函数总结
- php学习第二章:字符串处理函数(一)
- PHP学习笔记(三)-函数与数组
- php学习笔记(六)数组及其相关处理函数
- PHP中常用的字符串格式化函数总结
- 黑马程序员——零基础学习iOS开发——06 字符串、指针、指针和数组、指针和字符串、指针和函数
- php常用函数总结-----字符串