Android programmers learn PHP development (4)-variable variable variable reference-PhpStorm

黄舟
Release: 2023-03-06 09:44:01
Original
925 people have browsed it

No more mapping, the printing result is written directly in the code, which is clearer than the mapping.

<?php
    /**
     * 下面这4行,官方叫法,可变变量。
     * 我管它叫做,动态赋值变量名。
     * 只有在php里可以这么玩,因为在
     * php的变量名之前有$符。
     */
    $x = "a";
    $$x = "b";
    $$$x = "c";
    $$$$x = "d";

    echo $x; // 打印结果为a
    echo "<br>";
    echo $$x; // 打印结果为b
    echo "<br>";
    echo $$$x; // 打印结果为c
    echo "<br>";
    echo $$$$x; // 打印结果为d
    echo "<br>";
    echo $x; // 打印结果为a
    echo "<br>";
    echo $a; // 打印结果为b
    echo "<br>";
    echo $b; // 打印结果为c
    echo "<br>";
    echo $c; // 打印结果为d
    echo "<br>";
    /**不能打印$d,因为没有这个变量*/

    /**
     * 下面为变量引用
     */
    $m = 10;
    $n = 20;
    $y = &$m; // y引用m的值,等于说,y是m的别名
    echo $y; // 打印结果为10
    echo "<br>";
    $m = $n;
    echo $y; // 打印结果为20
    echo "<br>";

    /**
     * 变量引用1:
     * 如果引用的变量被释放,
     * 则保留释放前的值
     */
    unset($m); // 释放给定的变量

    if (isset($y)){ // 这个变量存在
        echo "这个变量存在"; // m被释放,但是y保存m释放前的值
        echo "<br>";
        echo $y; // 打印结果为20
        echo "<br>";
    }else{
        echo "这个变量不存在";
    }

    if (isset($m)){ // 这个变量不存在
        echo "这个变量存在";
    }else{
        echo "这个变量不存在"; // m被释放 所以不存在
        echo "<br>";
    }

    /**
     * 变量引用2:
     * 引用第二个变量,会自动解除与第一个变量的引用关系
     */
    $p = 66;
    $q = &$p; // q引用p的值,等于说,q是p的别名
    echo $q; // 打印结果为66
    echo "<br>";

    $r = 77;
    $q = &$r; // q引用r的值,等于说,q是r的别名
    echo $q; // 打印结果为66
    echo "<br>";

    $q = 88; // 我们改变q的值,看一下p、r会不会变化
    echo $p; // 打印结果为66,p没有变化,说明q和p解除了引用关系
    echo "<br>";
    echo $r; // 打印结果为88,r变成了q的值,说明q和r形成新的引用关系
    echo "<br>";
Copy after login

The above is the content of Android programmers learning PHP development (4)-Variable variable reference-PhpStorm. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!