PHP 编程语言手册 4:PHP 中的函数
1。 PHP 中的函数定义
在 Circle 类中,定义一个计算圆面积的函数: getArea
---->[utils/Utils.php]--------------------
<?php
class Utils{
/**
* 换行工具方法
* @param int $num 行数
* @param bool $line true <hr> false <br>
*/
public static function line($num = 1, $line = true)
{
for ($i = 0; $i < $num; $i++) {//for循环控制
if ($line) {//条件控制
echo "<hr>";
} else {
echo "<br>";
}
}
}
}
---->[base/Funtest.php]------调用--------------
<?php
include '../utils/Utils.php';
Utils::line();//默认一行 有线
Utils::line(2,false);//两行 无线
Utils::line(5);//五行 无线
复制代码
3.使用二维数组创建表
/** 创建表格
* @param $content 二维数组
* @param string $css 表格样式
* @return string
*/
public static function createTable($content, $css = "border='1' cellspacing='0' cellpadding='0' width='80%'")
{
$row = count($content);
$col = count($content[0]);
$table = "<table $css>";
for ($i = 0; $i < $row; $i++) {//for循环控制
$table .= "<tr/>";
for ($j = 0; $j < $col; $j++) {
$value = $content[$i][$j];
$table .= "<td>$value</td>";
}
$table .= "</tr>";
}
$table .= "</table>";
return $table;
}
|-- 使用---------------------------
<?php
include '../utils/Utils.php';
$content = [
["姓名", "年龄", "性别", "武器", "职业"],
["捷特", 24, "男", "黑风剑", "战士"],
["龙少", 23, "男", "控尊戒", "铸士"],
["巫缨", 23, "女", "百里弓", "弓士"],
];
echo Utils::createTable($content);
Utils::line();
$css = "border='5' cellspacing='0' cellpadding='0' width='80%' bgcolor=#81D4F5";
echo Utils::createTable($content, $css);//自定义表格样式
复制代码
4。变量范围
4.1 局部变量
局部变量不能在块外调用
|--- 方法中[局部变量-动态变量]在函数调用完成会释放------------------
function area1(){
static $inArea = true;
if ($inArea) {
echo "true<br/>";
} else {
echo "false<br/>";
}
$inArea = !$inArea;
}
area1();//true
area1();//true
area1();//true
|--- 方法中[局部变量-静态变量]在函数调用完成不会释放,在静态内存区----------------
function area1(){
static $inArea = true;
if ($inArea) {
echo "true<br/>";
} else {
echo "false<br/>";
}
$inArea = !$inArea;
}
area1();//true
area1();//false
area1();//true
复制代码
4.2 全局变量
|-- 方法体中不能使用外面定义的变量
$name = "toly";
function say(){
echo "My name is $name" ;//Undefined variable: name
}
say();
|-- 解决方,1,在方法体中使用global关键字对变量进行修饰
$name = "toly";
function say(){
global $name;
echo "My name is $name";
}
say();
|-- 解决方法2,使用$GLOBALS键值对获取全局变量
$name = "toly";
function say(){
echo "My name is " . $GLOBALS['name'];
}
say();
复制代码
5。按值传递和按引用传递
|-- 传入值,并不会导致原值污染
function add($target){
$target = $target + 1;
return $target;
}
$num = 10;
$res = add($num);
echo $res;//11
echo $num;//10
|-- 传引用,方法中对入参的修改会修改原值
function add(&$target)//取地址{
$target = $target + 1;
return $target;
}
$num = 10;
$res = add($num);
echo $res;//11
echo $num;//11
复制代码
6。其他特色
|-- 可变函数(函数的变量化)----------------
function add($x, $y){
return $x + $y;
}
$fun = "add";//函数变量化
echo $fun(3, 4);//7
|-- 函数作为入参(俗称回调) ----------------
function add($x, $y, $fun){
return $fun($x) + $fun($y);
}
echo add(3, 4, function ($i) {//此处通过匿名函数
return $i * $i;
});//25
|-- //此处通过create_function创建匿名函数(php7.2后过时)
echo add(3, 4, create_function('$i', 'return $i * $i;'));//25
|-- 通过 call_user_func 来调用函数--------感觉挺诡异
call_user_func("md5", "张风捷特烈");//20ce3e8f34f3a3dd732a150a36d41512
|-- 通过 call_user_func_array 来调用函数--------第二参是参数的数组
function add($x, $y){
return $x + $y;
}
echo call_user_func_array("add", array(3,4));//7
作者:张风捷特烈
链接:https://juejin.im/post/5c8a19d75188257dd56e7d91
来源:掘金版权所有
作者如需商业转载请联系作者获得授权。非商业转载请注明来源。
版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。