Home > Backend Development > PHP Tutorial > How to declare multiple variables in PHP? (Introduction to multiple methods)

How to declare multiple variables in PHP? (Introduction to multiple methods)

藏色散人
Release: 2023-04-03 18:30:02
Original
10079 people have browsed it

When we write PHP-related program code, declaring variables is a basic operation. So this article will introduce how to declare variables in PHP. Hope it helps friends in need.

First of all, everyone needs to understand what is a php variable? That is, variables in PHP are containers used to store information, similar to sets in mathematics. The assignment method is similar to algebra in mathematics. For example: a=1;b=2; then c=a b;c=3.

Note: Throughout PHP programming, these letters represent variables.

1. Set ordinary variables $a = "hi";

2. Static variables

static $a = 0 inside the function;

Note : Assigning the result of an expression in the declaration will cause parsing errors such as static $a =3 3; (error)

Static variables only exist in the local function domain (inside the function), after the function is executed , the variable value will not be lost and can be used for recursive calls

3. Assign a value to the variable:

$bar = &$foo;

Add the ampersand to the Before the assigned variable

Changing the new variable will affect the original variable. This assignment operation is faster

Note: Only named variables can be assigned addresses

$bar = &$a;
$bar = &$foo;

Changing the value of $bar can only change the value of variable foo, but not the value of a (the reference has changed)

4. Variable variables (symbol ($))

$$a = "world";

Both variables are defined: the contents of

$a is "hello" and the content of $hello is "world".

Therefore, it can be expressed as:

echo "$a ${$a}"; or echo "$a $hello"; they will both output: hello world

To use mutable variables with arrays, an ambiguity must be resolved. This is what the parser needs to know when writing $$a[1] that you want $a[1] As a variable, you still want to use $$a as a variable and take out the value with index [1] in the variable. The syntax to solve this problem is to use ${$a[1]}, use ${$a}[1] for the second case.

How to declare multiple variables in PHP? (Introduction to multiple methods)

#The above is an introduction to the relevant knowledge about how to declare variables in PHP. I hope it will be helpful to friends in need. If there are any shortcomings, I hope the netizens can give me suggestions.

The above is the detailed content of How to declare multiple variables in PHP? (Introduction to multiple methods). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template