search
HomeBackend DevelopmentPHP ProblemThere are several types of variable assignment in php

php变量赋值有三种:1、传值赋值,将一个表达式的值赋予一个变量,语法例如“$a=表达式”;2、引用赋值,将另一个变量和当前的变量指向同一个地址,语法为“$a=&$b”;3、计数赋值,PHP和JavaScript中的对象默认传值方式都是采用计数赋值的。

There are several types of variable assignment in php

本文操作环境:Windows10系统、PHP8.1版、Dell G3电脑

php变量赋值

传值赋值:

php变量其实就是一个地址的别名而已,如同$a就是一个内存中的地址的别名,比如是0x000011,$b就是另一个地址,$a = $b就是把0x000011地址内存里的数据取出来放到$b所指向的地址中,当unset一个变量以后,就会把这个变量名和地址之间的联系断掉,此时操作系统就会执行垃圾回收,把这个地址的数据清理掉。

引用赋值:

php的引用赋值就是把另一个变量和当前的变量指向同一个地址,当修改两个变量中的任何一个数值的时候都会造成两个数据的变化。

计数赋值:

和Javascript一样,对象都是采用计数赋值的,当$a = new object()以后,就会在栈上开一个变量$a,此时的$a的值是一个地址,指向堆上的一块内存,这块内存上边是这个对象的一个实例,当$b = $a以后就会使得$b中的值也是堆得内存的地址。此时如果你unset掉$a以后,只是把栈上的$a清理掉而已,堆上的内存依然存在,只有你把$b也unset掉,才会使得$a和$b都没办法指向到这个对象,此时这个对象就没有办法得到,在c语言里这个叫做内存泄露,在Java和C#里都有自动垃圾回收机制,这个泄露的内存都会被自动回收,php也是一样,也会在没有变量指向这个堆上的内存以后就会被自动回收掉。

1、传值赋值,例如$a=1,$b=$a等;

2、引用赋值,例如$a=&$b,即$a和$b在都指向了内存中的同一个存储变量值得地址;

3、引用计数传值,在php和js中的对象都是默认的传值方式都是引用计数传值,例子如下:

<?php
class Dog{
    public $name="小花";
    public $leg=4;
}
$a=new Dog;//此时,$a指向了内存中的一个地址(假设0XFFAD[1]),该地址又指向最终对象的值
$b=$a;//此时,$b和$a都指向了内存中的另一个地址(0XFFAD[2]),该地址又指向最终对象的值
var_dump($b->leg);//结果是int 4
$b=999;
var_dump($b);//结果是int 999;
var_dump($a);//此时的结果不是int 999,而是object(Dog)[1]

知识扩展:

这三种赋值方式分别在什么场合下出现呢?

传值赋值一般在普通的原子变量以及数组时候就是传值赋值,引用赋值在你加上&作用符的时候就会生效,计数赋值在php中在资源文件和对象中存在。

什么是变量

变量通俗的来说是一种容器。根据变量类型不同,容器的大小不一样,自然能存放的数据大小也不相同。在变量中存放的数据,我们称之为变量值。

PHP 中的变量用一个美元符号后面跟变量名来表示。变量名是区分大小写的。在PHP中变量的命名规则通常是以字母或下划线开头,由字母、数字、下划线组成。变量名通常由声明变量所代表意义的英文单词组成。单词与单词之间通过_分隔,或者第一个单词首字母小写,之后每个单词首字母大写。这种命名方式我们称之为驼峰命名法。良好的命名规范有助于提高我们代码的可读性。

<?php
    $name = &#39;屋脊猫&#39;;
    $_age = 1;
    $sex = &#39;男&#39;;       
    echo "姓名:{$name},年龄:{$_age}, 性别:{$sex}";
    $1sex = &#39;男&#39;;  // 错误,不能以数字开头
?>

变量的初始化是给变量设置一个默认值(或者是需要的值),初始化的过程中PHP会给变量分配存储空间以及将变量值所在存储空间地址保存在变量中。

推荐学习:《PHP视频教程

The above is the detailed content of There are several types of variable assignment in php. For more information, please follow other related articles on the PHP Chinese website!

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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.