php中&的运用

Jun 13, 2016 pm 01:02 PM
amp echo obj php

php中&的应用

举例:

?

$a = 1;
$b = &$a;
$b = 2;
echo $b; //2
echo $a; //2
Copy after login

?1

表示 传递给函数的是一个引用。
如果你确实想用引用传递。你需要 function a(&$obj) {}
才接受。不过不推荐

2
表示传递的是一个变量。 只是接收的时候是一个引用接收。 不推荐。

手册中说明。 php4.1 ,似乎是从这个版本开始,php引入了变量计数功能。 变量传递没有额外内存的支出。并且引用传递需要另外的引用计数, 所以不推荐引用传递, 如果变量不是很大的还,推荐用传值,别引用。 

从 php5开始 。所有 Obj 都是引用传递。 无论是否用 &=
并且 &= 在对Obj 操作的时候,会有一条警告信息。
所以, 通常情况下避免引用传递, 数组内引用传递更麻烦。
Copy after login
<span><pre class='brush:php;toolbar:false;'>&表示传地址
当需要对大数据进行操作的时候,传址节省内存,当需要函数多返回值的时候也可以引用传参
我给你网上找了一篇文章,你看看,应该可以帮助你理解 
Copy after login
参考:<span style="line-height: 22px;">http://blog.sina.com.cn/s/blog_4f9fc6e10100av1p.html</span>
Copy after login
指针即可以指内存地址,又可以指内存地址当中的值,引用是指两个变量指向同一块内存,就是名字不同而已。

?

Statement of this Website
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 Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles