php 传值与传引用的区别详解
在php中传值与传引用对于我们来讲是有比较大的区别的一个引用可以调用内存地址赋值了,这样只要内存地址中内容变化而赋值的变量也就变化了,付值只是把内存中值给其它变量而己.
传值:
函数参数压栈的是参数的副本.
任何的修改是在副本上作用.没有作用在原来的变量上.
传引用:
压栈的是引用的副本,由于引用是指向某个变量的,对引用的操作其实就是对他指向的变量的操作,(作用和传指针一样,只是引用少了解指针的草纸)
例子代码如下:
<?php function func1($a) { $a = $a + 1; } function func2(&$a) { $a = $a + 1; } $sample = 1; func1($sample); echo $sample; // 输出 1 $sample = 1; func2($sample); echo $sample; // 输出 2 ?> //例代码如下: <?php $num1 = 15; $num2 = & $num1; $num2 = 20; echo $num1; //输出20 ?> //再比如如下代码: <?php function func1($a) { $a = $a + 1; } function func2(&$a) { $a = $a + 1; } $sample = 1; func1($sample); echo $sample; // 输出 1 $sample = 1; func2($sample); echo $sample; // 输出 2 ?>
总结一下:传值的话,如果是非对象,会传一个值的拷贝,对这个变量做任何改动都不影响原值,传引用或者传对象,是传真实的内存地址,对这个变量做的改动会影响原值.
文章地址:
转载随意^^请带上本文地址!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
