标签:echo date else start sub field php5 sum 均值
//没有参数的自定义函数
function sum(){
for ($i = 0 ; $i
$sum += $i;
}
echo $sum;
}
sum();
echo ‘中国‘;
sum();
//////////////////////////////
//封装自定义函数,可以计算任意数字到另一个数字之间的和
function sum ($start, $end)
{
for ($i = $strart; $i
$sum += $i;
}
echo $sum;
}
$strart = 1;
$end = 100;
sum($strart,$end);
sum(1,10);
////////////////////////////////////////////////
//自定义函数,获取文件的扩展名
function getExtension($filename)
{
$extension = strtolower (substr($filename,strrpos($filename, ‘.‘) + 1));
echo $extension;
}
getExtension (‘a.gif.jpg.png‘);
////////////////////////////////////////////
//将任意的关联数组的键名和键值连接成如下结构
//$arr = [‘username‘ => ‘tom‘ , ‘password‘ => ‘123‘ , ‘age‘ => 22]
//连接的结果是:
//INSET INTO ?(username, password, age) VALUES(‘Tom‘, ‘123‘, ‘22‘)
//该自定义函数的名称为 insert
function insert($array)
{
$fields = implode(‘,‘, array_keys($array));
$values = "‘" . implode("‘,‘", $array) . "‘";
$sql = "INSERT INTO ?({$fields}) VALUES ({$values})";
echo $aql;
}
$array = [‘username‘ => ‘tom‘, ‘password‘ => ‘123‘, ‘age‘ => 22];
insert($array);
//将任意的关联数组的键名和键值连接成如下结构
//$arr = [‘username‘ => ‘tom‘ , ‘password‘ => ‘123‘ , ‘age‘ => 22]
//连接的结果是:
//UPDATE? SET username=‘‘Tom‘, password=‘123‘, age=‘22‘
//该自定义函数的名称为 update
function update($array)
{
foreach($array as $fields => $value){
$sql .= $fields . ‘=\‘‘ . $value . ‘\‘,‘;
}
$sql = substr($sql, 0, -1);
$sql = "UPDATE ? SET " .$sql;
echo $sql;
}
$arr = [‘username‘ => ‘tom‘, ‘password‘ => ‘123‘, ‘age‘ => 22];
update($arr);
//////////////////////////////////////////////
//产生随机字符的函数,长度默认为4位
function random($type, $length = 4)
{
//数字型
if($type == 1){
$chars = ‘0123456789‘;
}else if($type == 2){
//字母型
$chars = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz‘;
}else {
//混合型
$chars = ‘23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz‘;
}
$chars = substr(str_shuffle($chars), 0, $length);
echo $chars;
}
random(1);
random(1,6);
random(2);
//////////////////////////////////////////////
//可以计算至少两个数字以上的数字的平均值
//PHP5.6之后的版本
function avg(...$avg)
{
$sum = array_sum($arg);
$number = count($arg);
echo round($sum / $number,2);
}
avg(1,3);
avg(1,2,7);
avg(1,,2,3,4);
//PHP5.6之前的版本
function average()
{
print_r(func_get_args());
}
average(1,3);
average(1,3,5);
average(1,3,5,8);
////
function average()
{
echo ‘索引位置为1的参数值是:‘ , func_get_arg(1);
}
average(1,3);
average(1,3,5);
average(1,3,5,8);
//////////
function average()
{
echo ‘共有‘ , func_num_args(), ‘个参数‘;
}
average(1,2);
average(1,4,5);
average(1,9,5,8);
///////////php5.6之前的版本
function average()
{
$args = func_get_args();
$sum = array_sum($args);
$number = func_num_args();
echo $sum / $number;
}
average(1,2);
average(1,4,5);
average(1,,9,5,8);
function.php
标签:echo date else start sub field php5 sum 均值
原文地址:https://www.cnblogs.com/lujieting/p/12466340.html