search
Homephp教程php手册php 赋值和引用赋值使用说明

在php中赋值为变量赋值与引用赋值了,下面我来给各位同学介绍他们的一些基本用法与区别了。

1、值赋值:将赋值表达式的值复制到变量。例子:

 代码如下 复制代码

$number=15;
$age=20;
$sum=12+"15";//$sum=27


2.创建数组有变量赋值和调用函数两种方法,这里先讲前者。

使用变量赋值方法很简单,直接给一个数组变量赋值即可。

实例:

 代码如下 复制代码

  $lang[]="php";
 $lang[]="html";
 $lang[]="css";
 echo "$lang[0]
";
 echo "$lang[1]
";
 echo "$lang[2]
";
?>

三个赋值语句产生的数组内容:

0=>php

1=>html

2=>css

3、引用赋值:所创建的变量与另一个变量引用的内容相同。因此如果多个变量引用了同一内容,修改其中任一个变量,在其余的变量上都会有反映。在等号后面加一个&符号($val2=& $val1)就可以完成引用赋值或者将&符号放在引用的变量前面($val2= &$val1):

 代码如下 复制代码

$val1="hello";
$val2=& $val1;
$val2="goodby";
echo '$val1 is '.$val1."
";
echo '$val2 is '.$val2."
";
?>

$val1 is goodby
$val2 is goodby


foreach引用赋值的问题

代码:

 代码如下 复制代码

$a = array(
    'a' => 'aa',
    'b' => 'bb',
    'c' => 'cc',
);
foreach( $a  as &$v){
;
}
print_r($a);


foreach( $a  as $v){

}

print_r($a);


----------------------

仔细想想,其实是简单的引用问题,第一次foreach完毕,$v实际是$a['c']的引用,循环时,每次对$v的赋值都会会改变$a['c']的值,而最后一次对$v的赋值是$v=$a['c'],$a['c']在上一次被赋值为$a['b'],所以会会出现上述情况



教程链接:

随意转载~但请保留教程地址★

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

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

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.

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.