search
HomeBackend DevelopmentPHP TutorialAnalyze variable variables and variable functions in PHP

什么叫可变。在程序世界中,可变的当然是变量。常量在定义之后都是不可变的,在程序执行过程中,这个常量都是不能修改的。但是变量却不同,它们可以修改。那么可变变量和可变函数又是什么意思呢?很明显,就是用另一个变量来定义他们,这个变量是可变的呀!

可变变量
$a = 'hello';

$$a = 'world';

echo $a, ' ', $hello;

咦,我们没有定义\$hello这个变量呀。嗯,从表面上看我们确实没有定义这个变量,但请注意这个$$符号。$符号的意思就是定义变量,当我们在一个$符号后面跟上一个已经定义的变量名,那么这个变量的内容就成为了新的变量名。也就是说,$a的内容hello成为了一个新的变量名叫$hello,然后给它赋值为world。是不是感觉不太好理解,也不便于我们查看代码,这个问题我们最后再说。

当然,以下的用法要注意:

$a = 1;
$$a = 2;

echo $1; // Parse error: syntax error, unexpected '1'
echo ${1}; // ok

$a = ['b', 'c', 'd'];
$$a = 'f';

echo $b, $c, $d;
  • 数字类型不是合法的变量名,不能作为可变变量被定义
  • 但是利用{},是可以输出的,{}会获取{}内部的值并作为一个变量来解析,这里的{1}我们利用可变变量赋值成为了一个变量,直接输出是非常的,但放在{1}中就成为了一个可解析的变量名,我们可以简单的理解为{1}转换成了$'1',成为了一个正式的变量名
  • 数组当然是不行啦
  • 它们这样写都是不会报错的

使用对象就不行了,直接就会报错了,对象是不能进行可变变量的操作的。

class A {}
class B extends A {}

$a = new A();
$$a = new B(); // Catchable fatal error: Object of class A could not be converted to string
可变函数

可变函数其实也大同小异,当我们在一个变量的后面加上()时,PHP就会尝试将这个变量当做函数来解析。

function testA()
{
    echo "testA";
}

$a = 'testA';
$a(); // testA

可变变量是将一个字符串转换成了一个变量名,而可变函数则是将一个字符串当做函数名来调用。比如类中的方法,我们可以这样来调用:

class C
{
    public function testA()
    {
        echo "C:testA";
    }
    public function testB()
    {
        echo "C:testB";
    }
    public function testC()
    {
        echo "C:testC";
    }
}

$funcs = ['testA', 'testB', 'testC'];

$c = new C();
foreach ($funcs as $func) {
    $c->$func();
}

可变函数的这种特性和另外两个系统函数的关系非常紧密,它们是:call_user_func()和call_user_func_array(),Laravel中服务容器的核心实现就是使用了call_user_func_array()来实现依赖注入与控制反转的,这个等我们将来学习到的时候再说。

总结

看似很美好很灵活的可变变量与可变函数在我们实际的开发中却很少使用。究其原因当然是可读性不好,代码不仅是写给机器的,也是写给人看的,团队中人员的水平不齐的话过多的使用这两种特性会产生非常多的混乱情况。但是,很多框架代码中会使用这些特性,所以,这也是我们向更高层次迈进所必须要掌握的东西。不管怎么样,学就是了,能在业务场合中使用可变变量或者函数大大节约代码量写出精致易读的代码更能彰显我们的技术实力。

测试代码:
https://github.com/zhangyue0503/dev-blog/blob/master/php/201911/source/PHP%E7%9A%84%E5%8F%AF%E5%8F%98%E5%8F%98%E9%87%8F%E4%B8%8E%E5%8F%AF%E5%8F%98%E5%87%BD%E6%95%B0.php

参考链接:
https://www.php.net/manual/zh/language.variables.variable.php(推荐:《PHP视频教程》)

The above is the detailed content of Analyze variable variables and variable functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

Video Face Swap

Video Face Swap

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

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.