search
HomeBackend DevelopmentPHP TutorialA brief discussion on the characters available for PHP variables_PHP Tutorial

先来说说php变量的命名规则,百度下一抓一大把:
(1) PHP的变量名区分大小写;
(2) 变量名必须以美元符号$开始;
(3) 变量名开头可以以下划线开始;
(4) 变量名不能以数字字符开头.

其实所有编程都类似的命名规范就是:
1. 变量第一个字符最好是 字母或_,不能以数字开头
2. 第二个字符开始允许 数字,字母,_

好了,差不多就是这样了,但是这不是我们要说的重点。
今天我们说说 PHP 变量的可用字符,不仅仅是 数字,字母,_ 哦。

前几天QQ上一朋友发我一个shell,是加密过的,通篇乱码,不过上面有注释,叫做 “神盾加密” 好霸气的样子。
里面用了一些比较生僻的知识点,其中最明显的就是变量名,所以今天我们先从变量开始讲。

当然网上我也没找到权威的质料强有力的说明PHP的变量名可用字符的信息,所以我只能自己测试了。(英文不好,没办法谷歌到有利的证据)
先来看下我所用的方法,(如果你有更好的方法,希望分享下。)

<?<span php
</span><span if</span> (<span $_POST</span><span ) {
    </span><span $chr</span> = <span chr</span>(<span $_POST</span>['chr'<span ]);
    </span><span eval</span>('$'.<span $chr</span>."=1;"<span );
    </span><span echo</span> 'ok'<span ;
    </span><span exit</span><span ;
}
</span>?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.js"></script>
</head>
<body>
    <script>
    <span for</span>(<span var</span> i = 0x00; i <= 0xFF; i++) { <span //</span><span  0x00 - 0xFF  255个字符</span>
        $.ajaxSettings.async = <span false</span>; <span //</span><span  同步模式, 为了按顺序返回数据</span>
        $.post( "?", {<span chr</span>: i}, (<span function</span> (data) { <span //</span><span  post i 给 php 解析</span>
            data === 'ok' && console.<span log</span>( "\\x"+(i).toString(16) ); <span //</span><span  如果只返回 ok 说明能正常执行,否则会抛出异常</span>
<span         });
    }
    </span></script>
</body>
</html>

代码还算比较简单,PHP 部分只负责解析每一个字符当作变量名的执行结果是否会抛出溢出。
比如 字符 a 那么会解析  ('$a=1;');  这样的结果肯定没问题,所以不会抛出异常,返回结果就是 ok 字符。
如果 字符 - 那么会解析  ('$-=1;');  这明显是不对的,所以会抛出  : syntax error, unexpected '-', expecting T_VARIABLE or '$'  和 ok 字符。
而下面的 ajax 部分者正是利用返回结果是否为 'ok' 而判断是否是有效的变量名。
看看执行后的结果是什么吧:

"\x41, \x42, \x43, \x44, \x45, \x46, \x47, \x48, \x49, \x4a, \x4b, \x4c, \x4d, \x4e, \x4f, \x50, \x51, \x52, \x53, \x54, \x55, \x56, \x57, \x58, \x59, \x5a, \x5f, \x61, \x62, \x63, \x64, \x65, \x66, \x67, \x68, \x69, \x6a, \x6b, \x6c, \x6d, \x6e, \x6f, \x70, \x71, \x72, \x73, \x74, \x75, \x76, \x77, \x78, \x79, \x7a, \x7f, \x80, \x81, \x82, \x83, \x84, \x85, \x86, \x87, \x88, \x89, \x8a, \x8b, \x8c, \x8d, \x8e, \x8f, \x90, \x91, \x92, \x93, \x94, \x95, \x96, \x97, \x98, \x99, \x9a, \x9b, \x9c, \x9d, \x9e, \x9f, \xa0, \xa1, \xa2, \xa3, \xa4, \xa5, \xa6, \xa7, \xa8, \xa9, \xaa, \xab, \xac, \xad, \xae, \xaf, \xb0, \xb1, \xb2, \xb3, \xb4, \xb5, \xb6, \xb7, \xb8, \xb9, \xba, \xbb, \xbc, \xbd, \xbe, \xbf, \xc0, \xc1, \xc2, \xc3, \xc4, \xc5, \xc6, \xc7, \xc8, \xc9, \xca, \xcb, \xcc, \xcd, \xce, \xcf, \xd0, \xd1, \xd2, \xd3, \xd4, \xd5, \xd6, \xd7, \xd8, \xd9, \xda, \xdb, \xdc, \xdd, \xde, \xdf, \xe0, \xe1, \xe2, \xe3, \xe4, \xe5, \xe6, \xe7, \xe8, \xe9, \xea, \xeb, \xec, \xed, \xee, \xef, \xf0, \xf1, \xf2, \xf3, \xf4, \xf5, \xf6, \xf7, \xf8, \xf9, \xfa, \xfb, \xfc, \xfd, \xfe, \xff"

整理后发现是这样的16进制数据,当然看不懂没关系,看下转义后的结果:

"A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, _, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, , ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,  , &iexcl;, &cent;, &pound;, &curren;, &yen;, &brvbar;, &sect;, &uml;, &copy;, &ordf;, &laquo;, &not;, &shy;, &reg;, &macr;, &deg;, &plusmn;, &sup2;, &sup3;, &acute;, &micro;, &para;, &middot;, &cedil;, &sup1;, &ordm;, &raquo;, &frac14;, &frac12;, &frac34;, &iquest;, &Agrave;, &Aacute;, &Acirc;, &Atilde;, &Auml;, &Aring;, &AElig;, &Ccedil;, &Egrave;, &Eacute;, &Ecirc;, &Euml;, &Igrave;, &Iacute;, &Icirc;, &Iuml;, &ETH;, &Ntilde;, &Ograve;, &Oacute;, &Ocirc;, &Otilde;, &Ouml;, &times;, &Oslash;, &Ugrave;, &Uacute;, &Ucirc;, &Uuml;, &Yacute;, &THORN;, &szlig;, &agrave;, &aacute;, &acirc;, &atilde;, &auml;, &aring;, &aelig;, &ccedil;, &egrave;, &eacute;, &ecirc;, &euml;, &igrave;, &iacute;, &icirc;, &iuml;, &eth;, &ntilde;, &ograve;, &oacute;, &ocirc;, &otilde;, &ouml;, &divide;, &oslash;, &ugrave;, &uacute;, &ucirc;, &uuml;, &yacute;, &thorn;, &yuml;"

除了前面的 A-Z_a-z 是我们熟悉的,后面的那些乱七八糟的东西竟然也能当作正常的变量名,简直不可思议。
其实只是PHP拓展了变量名的字符范围,在 A-Z_a-z 之上,将变量可用字符范围拓展到了 \x7f-\xff。
所以,第一个字符范围应该是
那么第二个字符是否也是这样能,我们继续测试下。
将上面 php 代码里的  ('$'.."=1;");  改成  ('$a'.."=1;");  保存测试、

"\x9, \xa, \xd, \x20, \x30, \x31, \x32, \x33, \x34, \x35, \x36, \x37, \x38, \x39, \x41, \x42, \x43, \x44, \x45, \x46, \x47, \x48, \x49, \x4a, \x4b, \x4c, \x4d, \x4e, \x4f, \x50, \x51, \x52, \x53, \x54, \x55, \x56, \x57, \x58, \x59, \x5a, \x5f, \x61, \x62, \x63, \x64, \x65, \x66, \x67, \x68, \x69, \x6a, \x6b, \x6c, \x6d, \x6e, \x6f, \x70, \x71, \x72, \x73, \x74, \x75, \x76, \x77, \x78, \x79, \x7a, \x7f, \x80, \x81, \x82, \x83, \x84, \x85, \x86, \x87, \x88, \x89, \x8a, \x8b, \x8c, \x8d, \x8e, \x8f, \x90, \x91, \x92, \x93, \x94, \x95, \x96, \x97, \x98, \x99, \x9a, \x9b, \x9c, \x9d, \x9e, \x9f, \xa0, \xa1, \xa2, \xa3, \xa4, \xa5, \xa6, \xa7, \xa8, \xa9, \xaa, \xab, \xac, \xad, \xae, \xaf, \xb0, \xb1, \xb2, \xb3, \xb4, \xb5, \xb6, \xb7, \xb8, \xb9, \xba, \xbb, \xbc, \xbd, \xbe, \xbf, \xc0, \xc1, \xc2, \xc3, \xc4, \xc5, \xc6, \xc7, \xc8, \xc9, \xca, \xcb, \xcc, \xcd, \xce, \xcf, \xd0, \xd1, \xd2, \xd3, \xd4, \xd5, \xd6, \xd7, \xd8, \xd9, \xda, \xdb, \xdc, \xdd, \xde, \xdf, \xe0, \xe1, \xe2, \xe3, \xe4, \xe5, \xe6, \xe7, \xe8, \xe9, \xea, \xeb, \xec, \xed, \xee, \xef, \xf0, \xf1, \xf2, \xf3, \xf4, \xf5, \xf6, \xf7, \xf8, \xf9, \xfa, \xfb, \xfc, \xfd, \xfe, \xff"

发现结果多了好多字符,其实有一部分我们是要去掉的,比如 其实就是 空格,相当于  ('$a =1;');  而已,当然是能正常执行的。
除了空格,还有 \t\r\n 都去掉因为这些也是PHP语法说允许的 \t=,\n=,\r=,所以我们要去掉结果中的前4个数据, , ,
最终得到的结果其实只是多了  \x30, \x31, \x32, \x33, \x34, \x35, \x36, \x37, \x38, \x39  熟悉 ascii 的人也许一眼就看出来了,这就是数字
所以第一个字符范围应该是 对正则不熟的也许会觉得怎么不是 ,其实 \w 就是 0-9a-zA-Z_

也许有人会说  $; ${};  这样的变量呢?
我觉得这个已脱离了变量命名的范围了,不是么。

好了,关于 php 变量可用字符的知识点分享完毕了,如果有哪说的不对的,请留言,我会及时改正以免误导大家。

我的猜测: ascii 范围 0-127(-), latin1 范围 0-255(-),也许PHP就是将范围扩充到 latin1 字符集了,当然我没看过PHP源码,只能说是个猜想而已。

 

经 @holine 提醒,我去翻了官网手册,果然找到了,好吧,我还费了这么大的劲去测试、

查看资料 http://www.php.net/manual/zh/language.variables.basics.php

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/755382.htmlTechArticleFirst let’s talk about the naming rules of PHP variables. Baidu will grab a lot of them next: (1) PHP variables The name is case-sensitive; (2) The variable name must start with the dollar sign $; (3) The variable name can start with an underscore...
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
How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

How do you retrieve data from a PHP session?How do you retrieve data from a PHP session?May 01, 2025 am 12:11 AM

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

How can you use sessions to implement a shopping cart?How can you use sessions to implement a shopping cart?May 01, 2025 am 12:10 AM

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

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

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.