search
HomeBackend DevelopmentPHP TutorialPHP内核探索:变量概述_php技巧

现代编程语言中的基本元素主要有:变量,流程控制接口,函数等等。我能否不使用变量来编写程序呢? 这显然是可以的,例如:

复制代码 代码如下:

    echo "Hello AndHM";
?>

这个程序很简单,输出一个字符串内容。

就和我们仅仅使用二进制也能编程一样,不使用变量也能完成大部分的工作,不使用变量我们的程序将丧失极大的灵活性, 变量可以让我们将值存储起来,以便在程序的其他地方使用,或者通过计算保存新的值。 变量具有三个基本特性:

名称。变量的标示符。就像小狗一样,主人可能会给这些小狗起个喜欢的名称。 变量命名上,PHP继承了Perl的语法风格,变量以美元符号开始,后面跟变量名。 一个有效的变量名由字母或者下划线开头,后面跟上任意数量的字母,数字,或者下划线。 PHP同时还支持复合变量,也就是类似$$a的变量,它会进行两次的解释。这给PHP带来了非常灵活的动态特性。

类型。变量的类型,就像小狗的品种,不同的小狗血统可能会不一样,有的聪明, 有的会购物等等。 在很多静态语言中,变量在定义时就指定了,在程序运行过程中都不允许进行变更, 那如果你有一只能随便指定品种的小狗会不会很拉风呢;-) PHP就是这样,属于弱类型语言,可以随便赋予它任何类型的值。
值内容。 这是标示所代表的具体内容。这就像是实实在在的小狗的这个实物。 你可以给任何一条小狗起名为:小七,在编程语言中也是如此,你可以给变量赋予它 所能表示范围的值。不过在同一时间,变量只能有一个值。

PHP中组成变量名的字母可以是英文字母 a-z,A-Z,还可以是 ASCII 字符从 127 到 255(0x7f-0xff)。 变量名是区分大小写的。

除了变量本身,在PHP中我们经常会接触到与变量相关的一些概念,比如:常量,全局变量,静态变量以及类型转换等。 本章我们将介绍这些与变量相关的实现。其中包括PHP本身的变量低层存储结构以及弱类型系统的实现, 以及这些类型之间的相互转换等。
先看一段PHP代码:

复制代码 代码如下:

    $foo = 10;
    $bar = 20;
    function change() {
        global $foo;
        $bar = 0;
        $foo++;
    }
    change();
    echo $foo, ' ', $bar;
?>

运行代码会输出11 20。

可是为什么会有这样的输出呢?变量在PHP的内部是如何实现的呢? 变量的作用域又是怎么实现的呢? 这是本章将对围绕变量这个主题展开讨论,下面我们从最基本的变量实现开始。

不是所有编程语言中的变量的值都可以改变的。想想我们学过的数学中的变量。 他们的值也是不可改变的。例如: x + y = 10; 变量x和y的值是不能发生变化的。 在某个具体场景,也就是某个方程式中只有表示特定的值,变量的值不能改变的好处是: 这样就能尽可能少的产生副作用, 在Erlang语言中就是如此,它是一门函数式编程语言,非常值得学习。

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
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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment