
正文
php第二天-函数的用法及封装,变量范围,匿名函数,递归函数
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1.函数
<?php
function test($info){
return $info;
} echo test("hello") ?>
输出hello
2.函数实现一个累加
<?php
echo "用函数实现一个累加";
function add($a){
$sum = 0;
for($i = 0;$i <= $a;$i++){
$sum += $i;
}
return $sum;
}
echo "50的累加是:".add(50)."<br>";
echo "100的累加是:".add(100)."<br>"
?>
输出
用函数实现一个累加50的累加是:1275
100的累加是:5050
3.function_exists判断函数是否存在
<?php
function add($a){
$sum = 0;
for($i = 0;$i <= $a;$i++){
$sum += $i;
}
return $sum;
}
var_dump(function_exists('add'))
?>
4.局部变量
<?php
$a = 0;
function print_A(){
$a = 3;
echo "在函数中显示局部变量a值:$a <p>";
return $a;
}
$b = print_A();
echo "在函数外显示局部变量b值:$b <br>";
echo $a; ?>
输出
在函数中显示局部变量a值:3
在函数外显示局部变量b值:3
0
5.全局变量
<?php
$a = 0;
function print_A(){
global $a;
$a = 3;
echo "在函数中显示局部变量a值:$a<p>";
return $a;
}
$b = print_A();
echo "在函数外显示局部变量b值:$b <br>";
echo $a; ?>
输出
在函数中显示局部变量a值:3
在函数外显示局部变量b值:3
3
6.unset可以删除释放变量(内存中删除)
<?php
$a = 1;
function print_A(){
global $a;
unset($a);
$a = 3;
echo "在函数中显示局部变量a值:$a<p>";
return $a;
}
$b = print_A();
echo "在函数外显示局部变量b值:$b <br>";
echo $a; ?>
输出
在函数中显示局部变量a值:3
在函数外显示局部变量b值:3
1
7.静态变量
<?php
function test(){
$a = 0;
echo $a;
$a++;
}
test();
test();
test();
?>
输出000
<?php
function test(){
static $a = 0;
echo $a;
$a++;
}
test();
test();
test();
?>
输出012,因为static会在内存中申请
8.chr传入ascii码,ceil进一法取整
<?php
echo chr(97);
echo '<br>';
echo ceil(5.61);
echo '<br>';
?>
9.引用参数的函数
<?php
function test(&$arg){
$arg = 200;
}
$var = 100;
test($var);
echo $var;
?>
输出200
<?php
function test($arg){
$arg = 200;
}
$var = 100;
test($var);
echo $var;
?>
输出100
10.默认传参
<?php
function test($name = "123")
{
echo $name;
}
test();
echo "<br>";
test(456);
?>
输出123 456
11.可变个数参数函数
<?php
/**
声明一个函数more_args(),用于打印参数列表的值
虽然没有声明参数列表,但可以传入任意个数,任意类型的参数值
*/
function more_args() {
$args = func_get_args(); //将所有传递给脚本函数的参数当做一个数组返回
for($i=0; $i<count($args); $i++) { //使用for循环遍历数组$args
echo "第".$i."个参数是".$args[$i]."<br>"; //分别输出传入函数的每个参数
}
}
more_args("one", "two", "three", 1, 2, 3); //调用函数并输入多个参数
?>
输出
第0个参数是one
第1个参数是two
第2个参数是three
第3个参数是1
第4个参数是2
第5个参数是3
12.func_get_args()函数(返回一个数组,包含所有参数)
//var_export() 函数用于输出或返回一个变量,以字符串形式表示。
<?php
function test(){
var_export(func_get_args());
}
test(1,2,3,4,5);
?>
输出
array ( 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, )
13.func_num_args()函数(返回参数总数 )
<?php
function test(){
var_export(func_get_args());
echo "<br>";
var_export(func_num_args());
}
test(1,2,3,4,5);
?>
输出
array ( 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, ) 5
14.func_num_arg()函数(接收一个数字参数,返回指定参数)
<?php
function test(){
var_export(func_get_args());
echo "<br>";
var_export(func_num_args());
echo "<br>";
var_export(func_get_arg(1));
}
test(1,2,3,4,5);
?>
输出
array ( 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, ) 5 2
<?php
function test(){
for($i=0;$i<func_num_args();$i++){
echo "key==>".$i."value==>".func_get_arg($i);
echo "<br>";
}
}
test("one","two","three","four");
?>
输出
key==>0value==>one
key==>1value==>two
key==>2value==>three
key==>3value==>four
15.回调函数
回调函数就是指调用函数时并不是传递一个标准的变量作为参数,而是将另外一个函数作为参数传递到调用的函数中
15.1 call_user_func_array
<?php
function foobar($arg,$arg2){
echo __FUNCTION__,"got $arg and $arg2";
}
call_user_func_array("foobar", array("one","two"));
?>
输出
foobargot one and two
15.2 使用变量函数声明和应用的回调函数
<?php
function test($fun){
$fun("1");
}
function abc($num){
echo $num;
} echo test('abc');
?>
输出1
15.2 类静态函数和对象的方法回调
<?php
/* 声明一个类Demo,类中声明一个静态的成员方法fun() */
class Demo {
static function fun($msg1, $msg2) {
echo '$msg1 = '.$msg1;
echo '<br>';
echo '$msg2 = '.$msg2;
}
}
/* 声明一个类Test, 类中声明一个普通的成员方法fun() */
class Test {
function fun($msg1, $msg2) {
echo '$msg1 = '.$msg1;
echo '<br>';
echo '$msg2 = '.$msg2;
}
} call_user_func_array( array("Demo", 'fun'), array('cywl', '一寸一叶') );
echo "<br>";
call_user_func_array( array(new Test(), 'fun'), array('BroPHP', '学习型PHP框架') );
输出
$msg1 = cywl
$msg2 = 一寸一叶
$msg1 = BroPHP
$msg2 = 学习型PHP框架
16.匿名函数
匿名函数也叫做闭包函数,允许临时创建一个没有指定名称的函数。最通常用来作为回调函数参数的值
<?php
$test = function($name){
printf("hello %s\r\n",$name);
};
$test('php');
$test('world');
?>
输出
hello php hello world
17.递归函数
<?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
因为每个线程会生成一个子线程,然后子线程最后都会碰到0,所以就回收了回来,所以每一个都会回显
include( )和require( ) 函数
require( )将一个文件在预处理期间被导入,像把该文件粘贴到使用函数的地方。
include( )与require ( )几乎等价,区别在于在脚本执行时包含,当处理失败时,include( )产生一个警告而require( )则导致一个致命错误。







