笨鸟学php(五) 函数的声明与使用
1、函数的声明
<?php /** * function 函数名(参数1, 参数2.... ){ * 函数体; * 返回值; * } */ $sum = sum(3, 4); echo $sum; function sum($x, $y){ $sum = 0; $sum = $x * $x + $y + $y; return $sum; } ?>
2、变量的范围
局部变量: 在函数中声明的变量就是局部变量, 只能在自己的函数内部使用。
全局变量: 在函数外声明的变量就是全局变量, 在变量声明以后直到整个脚本结束前都可以使用, 包括在函数中和 { } 中都可使用。
2.1 知识点: PHP的变量分不出是声明还是使用
<?php $a = 10; function demo($a){ $a += 10; echo $a . '<br>'; } demo($a); echo $a; // 结果: 20, 10 // PHP的变量分不出 $a 是声明还是使用 ?>2.2 知识点: 函数中使用全局变量必须使用global关键字, 在声明global之后的变量才是全局的变量
<?php $a = 10; function demo() { global $a; $a += 10; echo $a . " demo <br>"; } function test() { global $a; $a += 5; echo $a . "test <br>"; } echo $a . " ----<br>"; // 10 ---- demo(); // 20 demo echo $a . " !!!!<br>"; // 20 !!!! demo(); // 30 demo echo $a . " @@@@<br>"; // 30 @@@@ test(); // 35 test echo $a . " ####<br>"; // 35 #### ?>
静态变量: 只能声明在函数中(类中), 不能在全局声明, 变量前使用static关键字修饰。
2.3 知识点: 静态变量在静态代码块中存储, 它的值可以一个函数多次调用之间共享, 但只在第一次调用函数时声明到内存
以后再调用时就不再声明而直接使用(和Java类似)。
<?php function test(){ static $a = 0; $a++; echo $a."<br>"; } test(); // 1 test(); // 2 test(); // 3 ?>
3、变量函数
其实这个和JS又很类似, 例如: 定义$var = hello, 下次使用$var()时就将寻找与变量值同名的函数hello()
<?php function one($a, $b){ return $a + $b; } function two($a, $b){ return $a * $a + $b * $b; } function three($a, $b){ return $a * $a * $a + $b * $b * $b; } //$var = one; //$var = "two"; $var = three; echo "结果:".$var(3, 4)."<br>"; // 91 ?>
4、系统函数
4.1 常规函数bool copy (string source, string dest)
4.2 带有mixed, mixed表示可以传任何类型的数据
bool chown (string filename, mixed user)
4.3 带有&参数的函数, 表示引用赋值, 这个参数不能传值, 只能传一个变量,然后函数将变量的值改变,
我们在使用这个变量时,值也是变化的(和Java中传引用又是一样的)
bool arsort (array &array [, int sort_flags])<?php $arr=array(1, 9, 5, 8, 3, 4); sort($arr); print_r($arr); ?>4.4 默认函数, 带有[]的函数, 表示这个参数是可选的,如果你传值了就使用你传的值,如果没有传值则使用默认值
bool arsort (array &array [, int sort_flags])
<?php function demo($a=1, $b=20, $c){ echo "### $a ### $b ### $c ###<br>"; } demo(8,9); ?>4.5 带有...的参数函数, ...表示可以传任意多个参数
int array_unshift (array &array, mixed var [, mixed ...])
<?php function demo(){ $sum = 0; for($i = 0; $i < func_num_args(); $i++){ $sum += func_get_arg($i); } return $sum; } echo demo(1, 2, 3, 4, 5, 6, 7, 8, 9); ?>
4.6 回调函数 带有callback, 就是调用这个函数时需要我们传一个函数进来(函数名,函数名字串)
array array_filter (array input [, callback callback])
<?php function demo($n){ if($n % 2== 0){ return true; }else{ return false; } } $a = array(1, 2, -3, 4, -5, 6, -7, 8, 9); print_r(array_filter($a, demo)); // Array ( [1] => 2 [3] => 4 [5] => 6 [7] => 8 ) ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Notepad++7.3.1
Easy-to-use and free code editor
