PHP编程语言手册四:PHP中的函数

2019-03-1419:31:30后端程序开发Comments2,009 views字数 3333阅读模式
1.PHP中函数的定义

在Circle类中,定义一个函数计算圆的面积:getArea文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9978.html

PHP编程语言手册四:PHP中的函数
---->[obj/Circle.php]----------------
<?php
namespace toly1994;
define("PI", 3.141592654);//定义常量
class Circle extends Shape{
    private $radius;
    public function __construct($radius) {
        $this->radius = $radius;
    }
    ...
    /**
     * 获取圆的面积
     * @return float|int
     */
    public function getArea(){
        $area = PI * $this->radius * $this->radius;
        return $area;
    }
}

|-- 对象使用函数(方法)
$circle = new Circle(10);
echo $circle->getArea();//314.15926541
$circle->setRadius(20);
echo $circle->getArea();//256.6370616
复制代码

函数名调用时竟然不区分大小写,方法也不支持重载,真是神奇的语言...文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9978.html


2.类的静态方法以及参数

创建一个工具类来换行文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9978.html

PHP编程语言手册四:PHP中的函数
---->[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.二维数组的使用来创建table
PHP编程语言手册四:PHP中的函数
/** 创建表格
 * @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 局部变量

局部变量不能在块的外面被调用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9978.html

|--- 方法中[局部变量-动态变量]在函数调用完成会释放------------------
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
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9978.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9978.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/9978.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定