Home Backend Development PHP Tutorial PHP变量标识符的一些守则

PHP变量标识符的一些守则

Jun 13, 2016 am 11:00 AM
array br echo lt

PHP变量标识符的一些规则

PHP变量标识符提供了非常特别的方便。但是,相当多的从事多年的PHP程序员却无法清楚它的具体用法,从而出错时,不知哪里错了。所以,现在总结一下。便于大家快速掌握。
PHP变量的运行机制是,将变量标识符$后的字符串,或表达式运算结果的字符串作为变量名,去变量池获取变量值。
可见,PHP相当于提供了一个变量的“名称指针”。它不同于C++的地址指针,因为,不会有空地址的不安全性。也不会有变量内存溢出的问题。所有这些PHP都给我们完成了。
变量标识符:
$:以后面的字符串为变量名,取同名变量。
?? $a='Hello';
?? $$a = 'world';
{}将其中的表达式解析成字串,并取此字串的变量,{}不能单独在表达式外部存在,否则就会当成流程控制而报错。这就是说, {}前必有$,或者外面必有引号。
echo $a, ${$a}; echo $a, $$a;

二者的区别: $总是寻找后面的第一个字串, {}是将内部的表达式解析为字串。PHP正是用这一方式实现了变量的“名称指针”。

${}:由{}返回字串交给$再处理。用途:对于表达式结果取变量。
?如:${$array[$i][$j]} , 如果使用 $$array[$i][$j]} 则PHP会找 $$array这个变量。而不是找$array[$i][$j]为结果的变量名的变量。

{}在函数中与表达式中
猜猜看:下面程序返回什么:

   $a='Hello';   $$a = 'world';   echo '1 ', $a, '{$a}', '</br>';   echo '2 ', $a, "{$a}", '</br>';   echo '3 ', $a, "{{$a}}", '</br>';      echo '4 ', $a, "${$a}", '</br>';      echo '5 ', $a, ${$a}, '</br>';    echo '6 ', $a, "{${$a}}", '</br>';    echo '7 ', $a, "{{${$a}}}", '</br>';    echo '8 ', $a, "$$a", '</br>';         echo '9 ', $a, "{a}", '</br>';        echo '10 ', $a, "${a}", '</br>';
Copy after login

?

结果:

?? echo '1 ', $a, '{$a}', '';
输出是:1 Hello{$a}?

//正常情况下,单引号是非执行字串,按原结果返回。但是:Smarty同样会将其进行解析!!所以, Smarty模板中, ‘{$a}’这样的表达式,仍可能出现的是你不想要的结果!!
假如:str_replace('{$foo} ',$foo, '{$foo}.some');
因为上面的原因:需要改为:
? str_replace(array('{', '$foo',} '),array('',$foo, ''), '{$foo}.some');

?? echo '2 ', $a, "{$a}", '';
输出是: 2 HelloHello
双引号中,无论是否有{},变量总会被解析。
?? echo '3 ', $a, "{{$a}}", '';??
输出是: 3 Hello{Hello}?
//所以,如果要输出带{}的结果,则需要加两层。
?? echo '4 ', $a, "${$a}", '';?
输出是: 4 Helloworld
?? echo '5 ', $a, ${$a}, '';
输出是: 4 Helloworld
// ${$表达式},无论外面是否带双引号,结果都是一样的?

?? echo '6 ', $a, "{${$a}}", '';?
输出是: 6 Helloworld
?? echo '7 ', $a, "{{${$a}}}", '';
输出是: 7 Hello{world}
//要输出带{}的结果,必须多加一层。
echo '8 ', $a, "$$a", '';
输出是: 8 Hello$Hello
//双引号中的$只执行一次。所以,结果就不是你所要的。

附注:{}还可用于数组,即对数组下标访问。即
$array[$i][$j] 与 $array{$i}{$j}是等价的。但正常,由于PHP文档中给出是的[]。而字符串是字节数组,所以,我们只有在用字节数组模式访问字符串时,才使用{}
$a='ux:Cache'; $a{2}=''; echo $a; 这个结果是什么,你知道吗?
echo '9 ', $a, "{a}", '';
输出是:9 Hello{a}
?? echo '10 ', $a, "${a}", '';
输出是:10 HelloHello可以见到, {}外有$, {}也会将结果送给$解析。

总结:
双引号中的$只会解析一次。不会进行多重解析。echo “$$a”,则要改为 echo “${$a}”;
{}遇$成为{$foo}就会被解析。要输出带{}的结果,则要{{$foo}}
$是向后寻找字符串,所以,数组或表达式的结果: 不能
$$array[$i][$j] 而是要 ${$array[$i][$j]}

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

Video Face Swap

Video Face Swap

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

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)

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

In today's era of rapid technological development, programming languages ​​are springing up like mushrooms after a rain. One of the languages ​​that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

Go language development essentials: 5 popular framework recommendations Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

&quot;Go Language Development Essentials: 5 Popular Framework Recommendations&quot; As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Jun 13, 2023 pm 05:01 PM

Laravel is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

Detailed explanation of the role and usage of the echo keyword in PHP Detailed explanation of the role and usage of the echo keyword in PHP Jun 28, 2023 pm 08:12 PM

Detailed explanation of the role and usage of the echo keyword in PHP PHP is a widely used server-side scripting language, which is widely used in web development. The echo keyword is a method used to output content in PHP. This article will introduce in detail the function and use of the echo keyword. Function: The main function of the echo keyword is to output content to the browser. In web development, we need to dynamically present data to the front-end page. At this time, we can use the echo keyword to output the data to the page. e

What are the most popular golang frameworks on the market? What are the most popular golang frameworks on the market? Jun 01, 2024 pm 08:05 PM

The most popular Go frameworks at present are: Gin: lightweight, high-performance web framework, simple and easy to use. Echo: A fast, highly customizable web framework that provides high-performance routing and middleware. GorillaMux: A fast and flexible multiplexer that provides advanced routing configuration options. Fiber: A performance-optimized, high-performance web framework that handles high concurrent requests. Martini: A modular web framework with object-oriented design that provides a rich feature set.

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Simple and clear method to use PHP array_merge_recursive() function Simple and clear method to use PHP array_merge_recursive() function Jun 27, 2023 pm 01:48 PM

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values ​​of the same keys, making the program design more flexible. array_merge

See all articles