search
HomeBackend DevelopmentPHP TutorialPHP 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)

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
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment