search
HomeBackend DevelopmentPHP ProblemDo you know how to set values ​​for PHP variables?

Do you know how to set values ​​for PHP variables?

Aug 09, 2021 am 11:58 AM
phpvariablevariable assignment

在之前的文章中我们介绍了使用PHP函数检测变量数据类型的方法,有需要的可以点击链接查看→《教你使用PHP函数来检测变量的类型》。这次我们来聊聊变量赋值,介绍一下在PHP中怎么给变量传值,一起来学习下。

首先我们需要了解一下变量,这样才能更好的了解变量的赋值方式。

变量通俗的来说是一种容器,是临时存储值的容器,它可以储存数字、文本、或者一些复杂的数据等;这些数据,就是“变量值”。且根据变量类型不同,容器的大小不一样,存放的数据大小也是不相同的。

因为PHP是一种弱类型的语言,所以使用变量前不用提前声明,在第一次赋值时会被自动创建。PHP中的变量用一个美元符号“$”后面跟变量名来表示(注:变量名是区分大小写的)。

例:

<?php
    $name = '西门庆';
    $_age = 25;
    $sex = '男';        
    echo "姓名:{$name},年龄:{$_age}, 性别:{$sex}";
?>

PHP变量名并不是可以随意定义的,它遵循一定的命名规则,通常是以字母(A~z)或下划线(_)开头,由字母、数字(0~9)、下划线组成。

例如下面的变量声明就是错误的:

$1sex = '男';  // 错误,不能以数字开头

一般PHP变量在声明时,就进行了赋值操作,下面我们就来了解一下PHP变量的赋值方式。

PHP中有两种传值方式,分别为传值赋值引用赋值

1、传值赋值

“传值赋值”是PHP中默认的传值方式,可以像上文那样使用“=”运算符直接将值赋给变量

<?php
    $a = 1; 
?>

也可以将一个变量的值,赋予给另一个变量。我们看看下面这个例子:

<?php
$a = 100;
$b = $a;    // 将$a的值复制一份,传给$b
echo '$a = '.$a;
echo '<br> $b = '.$b;
?>

输出结果:

$a = 100
$b = 100

需要注意的是:赋予变量值的方式,其实是将一个变量的值,“复制”一份,再传给另一个变量。本质上这两个变量是相互独立的,因此改变其中一个变量的值,将不会影响到另外一个变量。看看下面这个例子:

<?php
$a = 100;
$b = $a;    // 将$a的值复制一份,传给$b
$a = 200;   // 重新为 $a 赋值
echo '$a = '.$a;
echo '<br> $b = '.$b;
?>

我们来看看输出结果:

$a = 200
$b = 100

但有时我们就需要,改变其中一个变量的值,另外一个变量也跟着改变,这样怎么办?那就需要使用“引用赋值”了。

2、引用赋值

“引用赋值”简单来说就是:新的变量引用了原始的变量。它会将一个变量的内存地址,“复制”一份,传给另一个变量。

这也就意味着,两个变量是有关联的,改变了新的变量的值将影响到原始的变量的值,反之亦然。

“引用赋值”和“传值赋值”差不多,只是多了一步:在需要在被引用的变量(源变量)前加上“&”符号(C语言中称之为取地址符)。

<?php
$a = 100;
$b = &$a;     // 将$a的地址复制一份,传给$b
$a = 200;   // 重新为 $a 赋值
echo '$a = '.$a;
echo '<br> $b = '.$b;
?>

通过取地址符&,变量$a和变量$b的值指向了同一个内存地址,当我们修改其中一个变量的值时,另一个变量的值自然而然的随之被改变。我们来看看输出结果:

$a = 200
$b = 200

注:只有有名字的变量才可以引用赋值。

<?php
$foo = 25;
$bar = &$foo;      // 合法的赋值
$bar = &(24 * 7);  // 非法; 引用没有名字的表达式
?>

(24*7)这个表达式中因为没有保存在变量中,所以没有明确的内存地址,通过取地址符去获取在内存中存储的地址是不可取的。我们来看看输出结果:

Do you know how to set values ​​for PHP variables?

好了就说到这里了,有其他想知道的,可以点击这个哦。→ →php视频教程

最后给大家推荐一个PHP数组的免费视频教程:从0开始进入PHP的世界,快来学习吧!

The above is the detailed content of Do you know how to set values ​​for PHP variables?. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

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