Home Backend Development PHP Tutorial PHP study notes--about variables in php_PHP tutorial

PHP study notes--about variables in php_PHP tutorial

Jul 20, 2016 am 11:13 AM
php variable variable study manual notes

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>
?>
Copy after login

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>?>
Copy after login

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>?>
Copy after login

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>
Copy after login

 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>
?>
Copy after login

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)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440341.htmlTechArticleVariable variables in PHP (please refer to the php manual for more information): Variable variables in php are explained plainly. , which is to parse the value of a variable into a variable name and read the value of that variable name. Example: ...
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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles