PHP函数
//1.自定义函数-------------------------------------------
//函数的声明 计算折后价
function getPrice(float $price, float $discount) : float{
return $price*$discount;
}
//函数的调用
echo getPrice(200,0.7).'<hr>';
//2.系统函数,不需要声明直接调用即可---------------------------
//截取字符串
$str = 'hello PHP';
echo mb_substr($str,0,5).'<hr>';
//3.可变函数,将函数的名称放在一个变量中进行引用-----------------
$funName = 'getPrice';
echo $funName(100,0.5).'<hr>';
//4.匿名函数-----------------------------------------------
$sentence = '欢迎来到~';
$welcome = function (string $city) use ($sentence):string {
return $sentence.$city;
};
echo $welcome('上海').'!'.'<hr>';
// 闭包函数
$function = function ($discount) {
return function (float $money, int $n) use ($discount) : float {
$total = $money * $n;
return ($total > 500) ? ($total * $discount) : $total;
};
};
echo '应付金额:'.$function(0.9)(200, 5).'元<hr>';
运行结果
函数的返回值
// 函数的返回值,函数只支持单值返回。
//1.字符串拼接返回--------------------------------------------------------------------
function price( float $unitPrice, float $number):string {
$totalPrice = $unitPrice*$number; //商品原价
$finalPrice = $totalPrice >=500 ? $totalPrice*0.8 : $totalPrice; //商品折后价
$difference = $totalPrice-$finalPrice; //商品优惠金额
return '您的商品总价为'.$totalPrice.'元,优惠金额'.$difference.'元,最终价格为'.$finalPrice.'元';
}
echo price(200,3);
echo '<hr>';
// 2. 通过数组返回多值-------------------------------------------------------------------
function connect(): array {
$status = 1;
$message = '连接成功';
return ['status'=>$status, 'message'=>$message] ;
}
echo implode(', ', connect()), '<hr>';
//3. 通过jsons格式化字符串返回多值---------------------------------------------------------
function price2( float $unitPrice, float $number):string {
$totalPrice = $unitPrice*$number; //商品原价
$finalPrice = $totalPrice >=500 ? $totalPrice*0.8 : $totalPrice; //商品折后价
$difference = $totalPrice-$finalPrice; //商品优惠金额
return json_encode(['total' => $totalPrice,'final' => $finalPrice,'diff' => $difference]);
}
echo price2(1000,2), '<hr>';
//4. 将返回值序列化成字符串返回多个值------------------------------------------------------
function connect2(): string {
$status = 1;
$message = '连接成功';
// 将返回值序列化成字符串返回多个值
return serialize(['status'=>$status, 'message'=>$message]);
}
$str = connect2();
echo '序列化后的结果为--------:'.$str.'<br>';
$arr = unserialize($str);
echo '反序列化后的结果为--------:';
print_r($arr);
echo '<br>';
print_r($arr['message']);
运行结果:
函数的参数
//函数的参数
//1.值参数:值传递参数
function familyName($name){
return '张'.$name.'。'.'<br>';
}
echo '老大'.'~~'.familyName('函');
echo '老二'.'~~'.familyName('杰');
echo '老三'.'~~'.familyName('涛');
echo '<hr>';
//2.引用参数------------------------
function example( &$num )
{
$num = $num * 5;
echo "在函数内:\$num = ".$num;
}
$num = 5;
example( $num ) ;
echo "<p>在函数外:\$num = $num <p>" ;
echo '<hr>';
//默认参数-------------------------------
function person($name="张三", $age=20, $sex="男"){
echo "姓名:".$name.", 年龄:".$age.", 性别:".$sex. "<br>";
}
person();
person("老王");
person("老李",22);
person("阿苏",18 ,"女");
echo '<hr>';
//剩余参数---------------------------------
function sum(...$variable){
return array_sum($variable);
}
$arr = [1,2,3,4,5];
echo sum(...$arr);
运行结果:
回调函数
//内置回调函数call_user_func(),call_user_func_array()。
function demo($param1,$param2)
{
echo '函数'.__FUNCTION__.'被执行,传入的参数是:'.$param1.' '.$param2;
echo "<br/>";
}
//通过call_user_func调用函数demo
call_user_func('demo','1','2');
//通过call_user_func_array调用函数
call_user_func_array('demo',array('10','20'));
echo '<hr>';
//回调函数-------------------------------------------------
$data = range(1,20);
//array_map-------------
$arr = array_map(function ($item){
if($item%5 ===0){
return $item;
}
},$data);
print_r($arr);
echo '<hr>';
//array_filter---------
$arr2 = array_filter($arr);
print_r($arr2);
运行结果
函数的命名空间
//函数的命名空间
namespace first;
function getName(){
return __FUNCTION__;
}
echo getName().'<hr>';
namespace second;
function getName(){
return __FUNCTION__;
}
echo getName();
运行结果
字符串的创建方式
//字符串的创建方式 字符串的长度上限2G
//单引号和双引号创建变量
$single_quotes = 'This is a String';
$double_quotes = "This is a String";
echo '单引号~'.$single_quotes.'<br>';
echo '双引号~'.$double_quotes.'<br>';
echo '<hr>';
$var = 'String';
$single_quotes_var = 'This is a $var'; // 单引号中的变量不能被解析出来。
$double_quotes_var = "This is a $var"; // 双引号中的变量能被解析出来。
echo '单引号~'.$single_quotes_var.'<br>';
echo '双引号~'.$double_quotes_var.'<br>';
echo '<hr>';
echo '单引号~'.'hello \n\r word. <br>'; // 单引号的特殊字符不能被解析,原样输出
echo '双引号~'."hello \n\r word. <br>"; // 双引号的特殊字符能被解析,多个空格或回车在页面视为1个
echo 'This is a \'String\'. <br>'; // 单引号中又出现单引号,必须使用转义符: \
echo "This is a \"demo\". <hr>";
//heredoc语法创建变量,与双引号功能类似,heredoc的起始标识符 可以添加双引号。
$city = 'NAN JING';
echo <<< HEREDOC
<p>welcome to <span style="color:red">$city</span></p>
HEREDOC;
// nowdoc,起始标签必须添加单引号 ,适合大段的纯文本,不适合内嵌变量或特殊字符的文本
echo <<< 'NOWDOC'
hello php<br>hello java<br>hello mysql
NOWDOC;
运行结果