search
Homephp教程PHP开发php函数的传值与传址

php函数的传值与传址有什么区别呢?对于这样的问题我喜欢通过程序演示来获取答案。让我们来看一个演示记录吧!

<?php    
        
$i=100;    
        
function func($n){    
        
   $n=$n+100;    
           
   return $n;    
           
}    
echo &#39;1)函数传值前变量$i的值:&#39;.$i.&#39;<br>&#39;;    
echo &#39;2)传值后函数的返回值:&#39;.func($i).&#39;<br>&#39;;    
echo &#39;3)函数传值后变量$i的值:&#39;.$i.&#39;<br>&#39;;    
echo &#39;4)函数传址前变量$i的值:&#39;.$i.&#39;<br>&#39;;    
echo &#39;5)传址后函数的返回值:&#39;.func(&$i).&#39;<br>&#39;;    
echo &#39;6)函数传址后变量$i的值:&#39;.$i.&#39;<br>&#39;;    
//echo func($i).&#39;<br>&#39;;    
?>

    程序输出:
    1)函数传值前变量$i的值:100
    2)传值后函数的返回值:200
    3)函数传值后变量$i的值:100
    4)函数传址前变量$i的值:100
    5)传址后函数的返回值:200
    6)函数传址后变量$i的值:200
    
    解释:
    1)直接输出一个赋值$i=100的变量
    2)func函数传值返回一个算术相加的结果$=100+100
    3)func函数传值,其作用区域只限于函数内部,对外面的变量$i不会有影响
    4)同上
    5)func函数参数前多了个"&"符,表示传址,和2)一样,返回值是函数的算术运行结果。

    6)func(&$i),变量指针指向$i的位置,可以理解为是对变量$i的操作,$i=$i+100;这时变量$i已经相当于重新被赋值

    如果去掉上面代码的第18行注释,会得什么结果呢?
    小结:传址为了在执行函数的同时,同时改变函数参数值,而传值不期望改变。

          

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 Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

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