/**
* 生成验证码
* @param int $type 验证码类型 1、数字 2、字母 3、数字加字母
* @param int $length 验证码长度
* @return string
*/
function getCode($type=1, $length=4){
switch ($type) {
case 1:
$arr = range(0,9);break;
case 2:
$arr = range('a','z');break;
case 3:
$arr = array_merge(range(0,9),range('a','z'));break;
}
// 根据随机键名
$arr_rand = array_rand($arr,$length);
// 根据键名获取键值
foreach ($arr_rand as $key => $value) {
$str_rand .= $arr[$value];
}
return $str_rand;
}
<?php
function yzm($type,$length){
$num = '0123456789';
$word = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$result = '';
switch($type){
case 1: //生成数字类型验证码
for ($i=0;$i<$length;$i++){
$result .= $num[rand(0,strlen($num)-1)];
}
break;
case 2: //生产字母类型验证码
for ($i=0;$i<$length;$i++){
$result .= $word[rand(0,strlen($word)-1)];
}
break;
case 3: //生产字母+数字类型验证码
for ($i=0;$i<$length;$i++){
$incl = $num.=$word;
$result .= $incl[rand(0,strlen($incl)-1)];
}
break;
}
return $result;
}
echo yzm(3,4);
?>
获取文件扩展名
pathinfo($filename,PATHINFO_EXTENSION)
<?php
function eat($username)
{
echo $username.'吃西瓜....';
}
function dowhat($funcName,$param)
{
$funcName($param);
}
dowhat('eat','Mark');
echo "<hr /> 回调函数:";
$arr = [1,2,3,4,5,6];
array_walk($arr, function(&$val){
$val*=3;
return $val;
});
$res = array_map(function($val){return $val+100;}, $arr);
print_r($arr);
print_r($res);
echo "<hr /> 递归函数:";
function digui($i)
{
echo $i."<br />";
if($i>0){
$func = __FUNCTION__;
$func($i-1);
}
echo $i."<br />";
}
digui(3);
echo "<hr /> 获取文件扩展名:";
function getExt($filename)
{
return strtolower(pathinfo($filename,PATHINFO_EXTENSION));
}
echo getExt('text.php');
echo "<hr />";
?>
function_exists();检查函数是否存在。
array_pop();传引用,弹出最后数组中最后一个元素。
回调函数:
array_map();
array_walk();
array_filter();
call_user_func();
call_user_func_array();
传值对变量的更改不会影响变量本身,传引用则会影响变量本身。
get_defined_functions() 查看系统定义的函数。
回调函数:就是调用的时候,将另一个函数的名称,当作参数传递进去,并在函数体中进行调用。
<?php header("content-type:text/html;charset=utf-8"); /** * 默认产生可定位数的随机码 * type=1 数字 * type=2 字母 * type=3 数字+字母 */ function codes($type,$length){ $code = ''; //生成一个10个是数字,其余是字母的数组 $codearr = array_merge(range("0","9"),range("a","z"),range("A","Z")); switch ($type){ case '1': //全数字 for ($i=0;$i<$length;$i++){ $code .= $codearr[rand(0,9)]; } break; case '2': //全字母 for ($i=0;$i<$length;$i++){ $code .= $codearr[rand(10,count($codearr)-1)]; } break; case '3': //混合 for ($i=0;$i<$length;$i++){ $code .= $codearr[rand(0,count($codearr)-1)]; } break; } return $code."<br/>"; } echo codes('1',7); echo codes('2',6); echo codes('3',8); ?>
/*
*默认产生4位的数字的验证码
*type = 1 数字
*type = 2 字母
*type = 3 数字加字母
*可以改变验证码长度
* @param int $type 默认:1
* @param int $lenght 默认长度:4位数
*/
function getCode($type = 1, $lenght = 4){
$code = '';
$arr = [];
switch ($type) {
case '1':
$arr = array_merge(range(0, 9));
$arr_len = count($arr);
for($i=0; $i<=$lenght-1; $i++){
$mt = mt_rand(0, $arr_len-1);
$code .= $arr[$mt];
}
return $code;
break;
case '2':
$arr = array_merge(range('a', 'z'),range('A', 'Z'));
$arr_len = count($arr);
for ($i=0; $i<=$lenght-1; $i++) {
$mt = mt_rand(0, $arr_len-1);
$code .=$arr[$mt];
}
return $code;
break;
case '3':
$arr = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));
$arr_len = count($arr);
for ($i=0; $i<=$lenght-1 ; $i++) {
$mt = mt_rand(0, $arr_len-1);
$code .=$arr[$mt];
}
return $code;
break;
}
}
/** * @param int $type [验证码类型: 1.数字 2.字母 3.数字字母混合] * @param int $len [验证码长度] * @return string [ 返回验证码] */ function verificationCode($type = 1, $len = 6) { $num = range(0, 9); $str = array_merge(range('a', 'z'), range('A', 'Z')); $code = ''; for($i = 0; $i < $len; $i++) { switch($type) { case 1: $code .= mt_rand(0, 9); break; case 2: $code .= $str[mt_rand(0, 51)]; break; case 3: $code .= array_merge($num, $str)[mt_rand(0, 61)]; break; } } return $code; } echo verificationCode(3, 6);
自己封装函数的时候一定带上注释
/*
*@param number $num1
*@param number $num2
*@param string $op
*@return string
*/
封装获取当前时间日期的“形式”的函数(即显示形式为2018年5月3日,还是2018/5/3等)
主要用到内置函数date();
获取当前星期几?date('w');显示结果为0~6,0代表周日
获取当前年月日?date('Y-m-d');
想改变年月日之间的形式,是通过自定义函数的参数来控制,so easy!!!!!!!!!看截图即可
函数的返回值
可以用return返回,函数不能返回多个值,但可以通过返回一个数组来得到类似的效果。
echo 是直接打印,并没有让函数体运行,也就是说echo并不能返回函数的值;
<meta charset="utf-8">
<?php
/*验证码*/
function generateFourBitCode(){
$result = "";
for( $i = 0 ; $i < 4 ; $i++ ){
$n1 = mt_rand(48,57);
$n2 = mt_rand(65,90);
$n3 = mt_rand(97,122);
$arr = array($n1,$n2,$n3);
$result .= chr($arr[mt_rand(0,2)]);
}
return $result;
}
/*截取文件扩展名 方案1*/
function strSub1($strTxt1){
$str=explode(".", $strTxt1); //把字符串分割成字符串数组
$cou=sizeof($str); //计算数组长度
print_r($str[$cou-1]); //取最后一个字符串
// print_r($cou);
}
/*截取文件扩展名 方案2*/
function strSub2($strTxt2){
//echo strrev(explode('.', strrev($file))[0]); 等价下面
$strre=strrev($strTxt2); //把字符串倒过来
// print_r($strre);
$str=strtok($strre,"."); // 取得第一个.之前的字符
$strre1=strrev($str); //把取得的字符倒过来
echo $strre1;
}
/*截取文件扩展名 方案3*/
function strSub3($strTxt3){
echo substr(strrchr($strTxt3, "."),1); //这里不用substr会把.也弄进去
}
/*截取文件扩展名 方案4*/
function strSub4($strTxt4){
echo substr($strTxt4,strrpos($strTxt4, ".")+1);
}
/*截取文件扩展名 方案5*/
function strSub5($strTxt5){
echo end($str=explode(".", $strTxt5));
}
/*截取文件扩展名 方案6*/
function strSub6($strTxt6){
echo pathinfo($strTxt6,PATHINFO_EXTENSION); //pathinfo($strTxt6)['extension']; 效果一样
}
?>