变量名中的点 [英] dot in variable name

查看:98
本文介绍了变量名中的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在查询字符串中传递带点的变量.那么如何保留名称中带有点的变量名称

I am passing the variable with dot in query string.Php is replacing the dot with under score. So how can i retain the variable name which is having dot in the name

http://localhost/sample.php?one.txt = on& two.txt = on

sample.php

sample.php

$ ret = $ _ REQUEST ['one.txt'];//不起作用

$ret=$_REQUEST['one.txt'];//Not working

推荐答案

PHP将变量名从one.txt转换为one_txt的原因是因为变量名中的点无效.

The reason PHP is converting your variable name from one.txt into one_txt is because dots are not valid in variable names.

有关更多详细信息,请参见 PHP文档:

For more details, look at the PHP Documentation:

变量名称遵循相同的规则 与PHP中的其他标签一样.一个有效的 变量名以字母或 下划线,后跟任意数量的 字母,数字或下划线.作为一个 正则表达式 因此表示: '[a-zA-Z_ \ x7f- \ xff] [a-zA-Z0-9_ \ x7f- \ xff] *'

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

您可以考虑更改(从._)并检查$_REQUEST['one_txt'],也可以使HTML表单传递有效的变量名.

You can either account for the change (. to _) and check for $_REQUEST['one_txt'] or you can make your HTML form pass a valid variable name instead.

要跟进Michael Borgwardt的评论,以下是 PHP的文本有关处理来自外部来源的变量的文档:

To follow-up on Michael Borgwardt's comment, here's the text from PHP's documentation about handling variables from external sources:

输入变量名称中的点

Dots in incoming variable names

通常,PHP不会更改 变量名 传递到脚本中.但是, 应该注意的是,句点(句号, 句号)在中不是有效字符 一个PHP变量名.由于这个原因, 看一下:

Typically, PHP does not alter the names of variables when they are passed into a script. However, it should be noted that the dot (period, full stop) is not a valid character in a PHP variable name. For the reason, look at it:

<?php
$varname.ext;  /* invalid variable name */
?>

现在,解析器看到的是 名为$ varname的变量,后跟 字符串连接运算符, 然后是裸露的字符串(即 不带引号的字符串不匹配 任何已知的关键字或保留字) 'ext'.显然,这没有 预期的结果.

Now, what the parser sees is a variable named $varname, followed by the string concatenation operator, followed by the barestring (i.e. unquoted string which doesn't match any known key or reserved words) 'ext'. Obviously, this doesn't have the intended result.

因此,重要的是 请注意,PHP会自动 替换传入变量中的任何点 带有下划线的名称.

For this reason, it is important to note that PHP will automatically replace any dots in incoming variable names with underscores.

这确实是特定于PHP的东西.

It is indeed a PHP specific thing.

这篇关于变量名中的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆