time :march 19
content:函数的参数详解
表格的代码
<?php
/**
* 读取文件后缀名
* @param string $file
* @return string
*/
$fileName = '../test.txt';
function checkFileSuffix($file){
if(file_exists($file)){
echo '文件存在<br>';
return pathinfo($file, PATHINFO_EXTENSION);
} else {
echo '文件不存在';
}
}
echo checkFileSuffix($fileName);试下笔记功能
function getFileExtension($filename){
$pattern="/\.[a-zA-Z]+$/";
$subject=$filename;
preg_match($pattern,$subject,$matches);
return $matches[0];
}
echo getFileExtension("12.12.txt.php");
function getFilePath($filename)
{
$data = pathinfo($filename);
return $data['extension'];
}
echo getFilePath("adb.php.html");
参数中既有可选参数又有必选参数,必选参数必须在可选参数之前
-
函数必选参数的基础知识讲解,必选参数也叫形参
函数“可选参数”的基础讲解,可选参数也叫实参
PHP获取文件后缀名(提供7种方法):
$file = 'x.y.z.png';
echo substr(strrchr($file, '.'), 1);
解析:
substr(name,int):从指定位置开始向后取,从0开始计数
strrchr($file, '.')
strrchr() 函数查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符
2.$file = 'x.y.z.png';
echo substr($file, strrpos($file, '.')+1);
解析:strrpos($file, '.')
查找 "." 在字符串中最后一次出现的位置,返回位置 substr()从该位置开始截取
3.$file = 'x.y.z.png';
$arr=explode('.', $file);
echo $arr[count($arr)-1];
4.$file = 'x.y.z.png';
$arr=explode('.', $file);
echo end($arr); //end()返回数组的最后一个元素
5.$file = 'x.y.z.png';
echo strrev(explode('.', strrev($file))[0]);
6.$file = 'x.y.z.png';
echo pathinfo($file)['extension'];
解析:pathinfo() 函数以数组的形式返回文件路径的信息。包括以下的数组元素:
[dirname]
[basename]
[extension]
7.$file = 'x.y.z.png';
echo pathinfo($file, PATHINFO_EXTENSION);
总结:字符串截取2种,数组分割3种,路径函数2种
<?php
function creatTable($rows,$cols=3,$content){ //没有默认值的为必选参数,必选参数要放在可选参数之前
$table = "<table border='1' width='100%'>";
for($i = 1;$i<=$rows ; $i++) {
$table.= "<tr>";
for($j = 1 ; $j<=$cols ;$j++){
$table.="<td align='center'>{$content}</td>";
}
$table.= "</tr>";
}
$table.="</table>";
return $table;
}
echo createTable();//可选参数必填,填入时要注意顺序
函数命名不可重名,创建函数的时候,需要检测一下此函数名是否存在。
return代表返回值,调用函数时。echo函数名就可以了;
必选参数,放在可选参数前面
如何理解必选参数:调用函数时必须设置值
如何理解可选参数:调用函数时,若不设置值,他会用默认值