PHP study notes--about variables in php_PHP tutorial
Variable variables in PHP (please refer to the PHP manual for more information):
To put it bluntly, variable variables in php are to parse the value of a variable into a variable name and read the value of that variable name. Example:
<?<span php </span><span $a</span> = "China"; <span //</span><span 变量a</span> <span $b</span> = "a"; <span //</span><span 变量b</span> <span $China</span> = "I'm Chinese !"; <span //</span><span 变量China</span> <span $f</span> = "b"; <span //</span><span 变量f</span> <span echo</span> <span $a</span>."<br />"; <span //</span><span 输出 China</span> <span echo</span> $<span $a</span>."<br />"; <span //</span><span 输出 I'm Chinese --这里像要当做可变变量解析,必须在前面多加一个$符号</span> <span $a</span> = "f"; <span //</span><span 改变变量指向的名称(这里就是可变变量的应用)</span> <span echo</span> $<span $a</span>."<br />"; <span //</span><span 经过上面指向变量f后输出 b</span> <span $a</span> = "b"; <span //</span><span 同上</span> <span echo</span> $<span $a</span>."<br /><br />"; <span //</span><span 输出 a</span> <span echo</span> <span $b</span>."<br />"; <span //</span><span 输出 a</span> <span echo</span> $<span $b</span>."<br />"; <span //</span><span 输出 b</span> <span echo</span> $$<span $b</span>."<br /><br />"; <span //</span><span 输出 a</span> <span echo</span> <span $f</span>."<br />"; <span //</span><span 输出 b</span> <span echo</span> $<span $f</span>."<br />"; <span //</span><span 输出 a</span> <span echo</span> $$<span $f</span>."<br />"; <span //</span><span 输出 b</span> <span echo</span> $$$<span $f</span>."<br /><br />"; <span //</span><span 输出 a</span> <span $</span><span $a</span> = "China"; <span //</span><span 前面最后一个已经更改了变量为b所谓$$a=$b 也就是改变的$b的值</span> <span echo</span> <span $b</span>."<br />"; <span //</span><span 输出 China</span> <span echo</span> $<span $b</span>; <span //</span><span 输出 I'm Chinese</span> ?>
Note: Mutable variables cannot be applied to $this and superglobal variables (the scope of PHP variables is different from other high-level programming languages. See the code)
<?<span php </span><span $name</span> = 'man'<span ; $</span><span $name</span> = 'abc'; <span //</span><span 如果事先没有man这个变量。就新建一个man变量。 然后把abc赋值过去</span> $$<span $name</span> = 'def'<span ; </span><span echo</span> <span $man</span>."<br />"; <span //</span><span 输出abc</span> <span echo</span> <span $abc</span>; <span //</span><span 输出def</span> <span echo</span> "<br /> <hr />"<span ; </span><span function</span><span show() { </span><span global</span> <span $name</span>; <span //</span><span 这里的global并不是设置为全局变量。而是引用</span> <span echo</span> <span $name</span>."<br />"; <span //</span><span 输出man</span> <span } </span><span function</span><span showtwo() { </span><span //</span><span global $name; //echo $name."<br />";</span> <span echo</span> <span $GLOBALS</span>['name']; <span //</span><span 超全局变量数组</span> <span } show(); showtwo(); </span>?>
Variable function:
<?<span php </span><span function</span><span b() { </span><span echo</span> "这是B"<span ; } </span><span function</span> c(<span $name</span> = "China") <span //</span><span 设默认值</span> <span { </span><span echo</span> "这是<span $name</span>"<span ; } </span><span $a</span> = 'b'<span ; </span><span $a</span>(); <span //</span><span 找值所在的函数</span> <span $a</span> = 'c'<span ; </span><span $a</span><span ();<br /> </span>?>
A typical application of variable variables:
<!DOCTYPE html <span PUBLIC</span> "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <div> <form action="#" method="post"> <label>name: </label> <input type="text" name="name" /><br /> <label>pwd : </label> <input type="text" name="pwd" /><br /> <label>tag : </label> <input type="text" name="tag" /><br /> <input type="submit" value="提交" /> </form> </div> <?<span php </span><span foreach</span>(<span $_POST</span> <span as</span> <span $key</span>=><span $value</span><span ) { </span><span //</span><span print_r($_POST);</span> $<span $key</span> = <span $value</span><span ; } </span><span //</span><span extract($_POST); //从数组中将变量导入到当前的符号表 --自行查找php手册</span> <span echo</span> <span $name</span>."<br />"<span ; </span><span echo</span> <span $pwd</span>."<br />"<span ; </span><span echo</span> <span $tag</span>."<br />"<span ; </span>?> </body> </html>
Supplement:
Characteristics of variables. If a variable has not been declared in advance, then if you want to assign a value to a variable, one of the operations of PHP in the background is that when you assign a value to the undeclared variable, the background has already declared the variable for you. Just look at the example:
<?<span php </span><span class</span><span A { </span><span public</span> <span function</span><span show() { </span><span //</span><span 记住这里的name 实现是没有声明的。</span> <span echo</span> (<span isset</span>(<span $this</span>->name)?"true":"false")." -- "<span ; </span><span echo</span> <span $this</span>-><span name; } } </span><span $A</span> = <span new</span> A(); <span //</span><span 实例化 //直接输出,是没有任何结果的. 因为没有这个变量 。。这里可以用isset判断为false</span> <span $A</span>->show(); <span //</span><span 输出 "fase -- " //这里进行赋值,在赋值时,后台默认声明此变量</span> <span $A</span>->name = "我有输出了。这个变量被声明了!"<span ; </span><span echo</span> "<br />"<span ; </span><span $A</span>->show(); <span //</span><span 输出 "true -- 我有输出了。这个变量被声明了!"</span> ?>
Summary: After reading the above example, don’t be surprised if you see other people’s encapsulated code in the future and use it directly without declaring variables. That means you have to set it up yourself. You can just assign it directly. . . (PS: In fact, I was confused at first, because those who used to work on .NET would never allow this to happen in C#. I am used to using strong-type languages... When I look at this weak-type language, It’s true that you won’t get used to it very well at first)

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

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

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

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
