简体   繁体   English

奇怪的PHP字符串错误

[英]Strange PHP string error

This is a very very weird bug that happens randomly. 这是一个非常奇怪的错误,它随机发生。 So I will try to explain this as best as I can. 因此,我将尽力解释这一点。 I'm trying to diminish the code substantially to pinpoint the problem without having to reference a framework, nor deal with 500 lines of code outside of the framework - so I will try and come back and update this question when I do. 我正在尝试大幅减少代码以查明问题,而不必引用框架,也无需在框架外处理500行代码-因此,我将尝试返回并更新此问题。 OK, so here we go. 好的,我们开始。

So basically say I'm taking a string in a function and preg_replace like so: 所以基本上说我正在像这样在函数和preg_replace中使用字符串:

$table = '/something';
print $table;
$table = preg_replace("@^(/)@",'prefix_',$table);
print $table;

The output will look something like 输出看起来像

...
/something - prefix_something
/something - prefix_something
/something - /something

You have an error in you sql query... blah blah somewhere around SELECT * FROM /something

What happens is that after rolling through the code, this simply stops working and the sql errors out. 发生的事情是,在滚动代码之后,这只是停止工作,并且sql错误消失了。 What's more strange is that if I start commenting out code outside the function it then works. 更为奇怪的是,如果我开始注释掉函数外的代码,那么它将起作用。 I put that code back in, it breaks. 我把代码放回去,它坏了。

I tried recoding this by substr($var,0,1) and var_dumping the variable. 我尝试通过substr($var,0,1)和var_dumping变量重新编码。 After a few rounds it returns a bool(false) value even though $var is in fact a string. 几轮后,即使$ var实际上是一个字符串,它也会返回bool(false)值。 It's very very strange. 非常非常奇怪。 Has anybody run into this before? 有人遇到过吗? Is this a PHP bug? 这是PHP错误吗? It's looking like it to me. 在我看来。

-- EDIT -- -编辑-

Example: 例:

foreach ( $user as $id => $user ) {
  get_data("/table_name","*", "user_id = '$id'");
}


#in the function $_table_ holds "/table_name"
    $_table_ = trim($_table_);
    $tmp = explode(',', $_table_);
    $tables = array();
    foreach ($tmp as $c => $n) {
        $n = trim($n);
        $a = substr($n, 0, 1);
        $b = substr($n, 1);
        if ($a == false) {
            var_dump($n);
            $a = substr($n, 0, 1);
            var_dump($b);
            var_dump($a);
        }
        if ($a == '/') { etc...

Output: 输出:

string(11) "/table_name" string(10) "table_name" bool(false)

If I switch to the original preg_replace functionality, it simply does not preg_replace and the same thing happens "/table_name" is return rather then "prefix_table_name" and this happens randomly. 如果我切换到原始的preg_replace功能,那么它根本就不会preg_replace,并且会发生相同的情况,即返回“ / table_name”,而不是“ prefix_table_name”,并且这是随机发生的。 Meaning it will work on the string it broke on if I comment code out outside of the function that is seemingly unrelated. 这意味着,如果我在看似无关的函数之外注释掉代码,它将对断断续续的字符串起作用。

--- SOLUTION ---- - 解决方案 ----

I found out that it has to do with string to array conversion in another function that caused this strange error. 我发现这与导致此奇怪错误的另一个函数中的字符串到数组转换有关。

Basically a function was expecting a null or array() value and instead a number was passed. 基本上,一个函数期望一个nullarray()值,而是传递了一个数字。 We have notices turned off, so I turned them on and started fixing all the notices and warnings and that's how I found this strange bug. 我们已关闭通知,因此我将它们打开并开始修复所有通知和警告,这就是我发现此奇怪错误的方法。 Now exactly what the inner workings are that created the problem is unknown to me. 现在,确切地讲,造成问题的内部原因是什么,对我来说还是未知的。 However I can define the symptom as a string assignment problem that would occur at some other place in the code which we caught when it wouldn't reassign the $table variable with the new preg_replace value which is detailed above. 但是,我可以将症状定义为字符串分配问题,该问题将在代码中的其他位置发生,当我们不使用新的preg_replace值重新分配$ table变量时,该问题将在上面捕获,上面已详细介绍。 Now you know. 现在你知道了。 The function looks something similar to this: 该函数看起来类似于以下内容:

function format_something($one, $two, $three = array()){
    if ($three['something'] == 'Y') {
    /** etc... */
}
...
format_something('one','two',3);

After a while in a loop this type of conversion started having problems randomly in the code. 循环一段时间后,这种类型的转换开始在代码中随机出现问题。

substr doesn't always return a string. substr并不总是返回字符串。 If it can't find the substring, it returns false. 如果找不到子字符串,则返回false。

Return Values 返回值

Returns the extracted part of string, or FALSE on failure or an empty string. 返回提取的字符串部分,如果失败或为空字符串,则返回FALSE。

You should test for this condition and var_dump the string you are searching to see what it contains in such cases. 您应该测试这种情况,然后在搜索到的字符串中var_dump以查看其包含的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM