search
Homephp教程php手册php的变量和函数前加&的意思

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 任何其它表达式都不能通过引用传递,结果未定义.例如下面引用传递的例子是无效的: ?php function bar(){ // Note the missing $a = 5; return $a; } foo(bar()); foo($a = 5) // 表达式,不是变量 foo(

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

 

    任何其它表达式都不能通过引用传递,结果未定义.例如下面引用传递的例子是无效的:

   

    function bar(){ // Note the missing &

    $a = 5;

    return $a;

    }

    foo(bar());

    foo($a = 5) // 表达式,不是变量

    foo(5) // 常量,不是变量

    ?>

    这些条件是 PHP 4.0.4 以及以后版本有的.

    引用返回

    引用返回用在当你想用函数找到引用应该被绑定在哪一个变量上面时.当返回引用时,使用此语法:

   

    function &find_var ($param){

    /* ...code... */

    return $found_var;

    }

    $foo =& find_var ($bar);

    $foo->x = 2;

    ?>

    本例中 find_var 函数所返回的对象的属性将被设定(译者:指的是 $foo->x = 2; 语句),而不是拷贝,就和没有用引用语法一样.

    注: 和参数传递不同,这里必须在两个地方都用 & 符号 - 来指出返回的是一个引用,而不是通常的一个拷贝,同样也指出 $foo 是作为引用的绑定,而不是通常的赋值.

    取消引用

    当你 unset 一个引用,只是断开了变量名和变量内容之间的绑定.这并不意味着变量内容被销毁了.例如:

   

    $a = 1;

    $b =& $a;

    unset ($a);

    ?>

    不会 unset $b,只是 $a.

    再拿这个和 Unix 的 unlink 调用来类比一下可能有助于理解.

    引用定位

    许***PHP 的语法结构是通过引用机制实现的,所以上述有关引用绑定的一切也都适用于这些结构.一些结构,例如引用传递和返回,已经在上面提到了.其它使用引用的结构有:

    global 引用

    当用 global $var 声明一个变量时实际上建立了一个到全局变量的引用.也就是说和这样做是相同的:

   

    $var =& $GLOBALS[“var”];

    ?>

    这意味着,例如,unset $var 不会 unset 全局变量.

    $this

    在一个对象的方法中,$this 永远是调用它的对象的引用.

  [1] [2] 

php的变量和函数前加&的意思

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

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

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.