开发者

Strange PHP string error

开发者 https://www.devze.com 2023-03-05 18:10 出处:网络
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

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. OK, so here we go.

So开发者_运维问答 basically say I'm taking a string in a function and preg_replace like so:

$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. 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. After a few rounds it returns a bool(false) value even though $var is in fact a string. It's very very strange. Has anybody run into this before? Is this a PHP bug? 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. 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. 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. 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. If it can't find the substring, it returns false.

Return Values

Returns the extracted part of string, or FALSE on failure or an empty string.

You should test for this condition and var_dump the string you are searching to see what it contains in such cases.

0

精彩评论

暂无评论...
验证码 换一张
取 消