gpt4 book ai didi

php - 变量名中的点

转载 作者:可可西里 更新时间:2023-11-01 12:23:00 25 4
gpt4 key购买 nike

我在查询字符串中传递带有点的变量。Php 将点替换为低于分数。那么我怎样才能保留名称中有点的变量名

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

示例.php

$ret=$_REQUEST['one.txt'];//不工作

最佳答案

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

有关更多详细信息,请查看 PHP Documentation :

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 表单改为传递有效的变量名称。

编辑:

为了跟进 Michael Borgwardt 的评论,这里是来自 PHP's documentation about handling variables from external sources 的文本:

Dots in incoming variable names

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 */
?>

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.

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

这确实是一个特定于 PHP 的东西。

关于php - 变量名中的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1057622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com