> For the complete documentation index, see [llms.txt](https://phper.shujuwajue.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://phper.shujuwajue.com/ji-chu/han-shu.md).

# 函数

## 函数

### 变量的作用域

变量的作用域也称变量的范围，变量的范围即它定义的上下文背景（也是它的生效范围）。大部分php变量只有一个单独的范围。这个单独的范围跨度同样包含了include和require引入的文件。

```
$outer = 'str'; // 全局变量
function myfunction(){
    echo $outer;  // 局部变量
}
myfunction();
```

全局变量是不能拿到函数体内部使用的，两个$outer并不是一个，所以写程序应该避免这种同名混淆

如果内部想访问使用外部的那个全局变量，但是极力不推荐，如下面两种方式；

```
$outer = 'str'; // 全局变量
function myfunction(){
    global $outer;
    echo $outer;  // 局部变量
}
myfunction();
```

```
$outer = 'str'; // 全局变量
function myfunction(){
    echo $GLOBALS['outer'];  // 局部变量
}
```

### 静态变量

static关键词

* 1.仅初始化一次
* 2.初始化时需要赋值;
* 3.每次执行函数该值会保留
* 4.static 修饰的变量是局部的，仅在函数内部有效
* 4.可以记录函数的调用次数，从而可以在某些条件下终止递归

```
function myFunction() {
    static $a = 1;
    echo $a++;
}
myFunction(); // 1
myFunction(); // 2
myFunction(); // 3
```

示例：

```
$count = 5;
function get_count()
{
    static $count;  
    return $count++;
}

echo $count; // 5
++$count;

echo get_count(); // null
echo get_count();  // 1
```

结果`51`

* 1.函数内部和外部的count变量不是同一个
* 2.第一次调用函数时，默认没有赋值的count变量是null，后++，先返回后计算，所以仍然为null
* 3.第三次时候，返回上次计算过后的null ++ 结果返回（注意：null++的值是1，而null--的值还是null）；

### 函数的参数及参数的引用传递

默认情况下，函数参数通过值传递

如果希望允许函数修改它的值，必须通过引用传递参数

```
<?php

$count = 5;
get_count($count);
function get_count(&$var)
{
    $var = 1;
}

echo $count;
```

结果为1

避免使用如下方式：

```
<?php

$count = 5;
get_count(&$count);
function get_count($var)
{
    $var = 1;
}

echo $count;
```

中会得到一条警告说“Call-time pass-by-reference”已经过时了。详情[官方文档](http://www.php.net/manual/zh/language.references.pass.php)

### 函数的返回值及引用返回

**函数的返回值：**  <br>

* 值通过使用可选的返回语句（return）返回
* 可以返回包括数组和对象任意类型
* 返回语句会终止函数执行，将控制权交回函数调用处

从函数返回一个引用，必须在函数声明和指派返回值给一个变量时都使用引用运算符&

如下示例：

```php
<?php
function &myFunc(){
    static $b = 10;
    return $b;
}
$a = myFunc();
$a = &myFunc();
$a = 100;
echo  myFunc();
```

### 外部文件的引入

include/require语句包含并运行指定文件

如果给出路径名按照路径来找，否则从include\_path中查找

如果include\_path中也没有，则从调用脚本文件所在的目录和当前工作目录下寻找

当一个文件被包含时，其中所包含的代码继承了include所在行的变量范围

**区别**

* 加载过程中未找到文件则include结构会发出一条警告，这一点和require不同，后者会发出一个致命错误
* require 在出错时产生E\_COMPILE\_ERROR级别的错误。换句话说将导致脚本中止而include只产生警告（E\_WARNING),脚本会继续运行
* require(include)/require\_once(include\_once)唯一区别是PHP会检查该文件是否已经被包含，如果是则不会再次包含。

### 总结：

```php
<?php
$var1 = 3;
function myfuntion()
{
    global $var1;
    $var1 += 2; // 7
}
myfuntion();
echo $var1;
die;


$var1 = 5;
$var2 = 10;

function foo(&$my_var)
{
    global $var1;//5
    $var1 += 2; // 7
    $var2 = 4; //4
    $my_var += 3; //8
    return $var2; //4
}

$my_var = 5;
echo foo($my_var). "\n"; //4
echo $my_var. "\n"; //8
echo $var1; //7
echo $var2; //10
$bar = 'foo';
$my_var = 10;
echo $bar($my_var). "\n"; //4
die;

function &myFunc(){
    static $b = 10;
    return $b;
}
$a = myFunc();
$a = &myFunc();
$a = 100;
echo  myFunc();
```

> 特别注意上面函数内部global的问题，运行函数，同时会改变外部变量的值，得出7。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://phper.shujuwajue.com/ji-chu/han-shu.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
