search
Homephp教程php手册php函数变量的作用域及函数返回值教程

php函数变量的作用域及函数返回值教程

Jun 13, 2016 am 11:17 AM
phpScopevalueaboutfunctionparametervariableexistdefinitionTutorialofreturn

在php中关于在一个函数中定义的变量,包括参数在内都不能访问函数外部变量,并且在默认的情况下,在一个函数外部定义的变量不能访问的函数变量。  

在php教程中关于在一个函数中定义的变量,包括参数在内都不能访问函数外部变量,并且在默认的情况下,在一个函数外部定义的变量不能访问的函数变量

下面看实例

$a = 1;
$b = 2;
function Sum()
{
    global $a, $b;
    $b = $a + $b;
}
Sum();
echo $b;
?>

这样返回$b的值为3,在php中global是一个全局变量所有才会这样,那么我们现来看php变量引用实例

function str_unite (&$string)
{
    $string .= '也喜欢蓝色.';
}
$str = '喜欢红色,';
str_unite ($str);
echo $str;    // 输出结果: '喜欢红色,也喜欢蓝色.'
?> 

,上面讲的是函数的作用域的全局变量与函数的引用,下面来看个函数的局部变量

$a = 1;
$b = 2;
function Sum($a,$b)
{
     $b = $a + $b;

     echo $b;//3
}
Sum();//
echo $b;//2
?>

本站原创教程转载注明来源www.bkjia.com/phper/php.html


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

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),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment