PHP 7 is breaking all my code: Array to string conversion in

Since I moved to PHP 7 it has been a nightmare of errors for all my old code. The following is the latest one and I am not seeing what the problem is. Notice : Array to string conversion in…

All I am doing is sending two values to a function and returning a value to be displayed. I can’t post the site but here is a small snippet of what shows:

Strength: 996,904 [Rank: Array] …this should be a number.

Here is my code:

echo “number_format($ir[‘strength’]).”  [Rank: “.get_rank($ir[‘strength’], ‘strength’).”]";

It is the get_rank part where I am getting the error.

$ir[‘strength’] is a number retrieved from mysql and ‘strength’ is just a string that corresponds to the name in the mysql table.

This is the get_rank function:

function get_rank($stat, $mykey) {
global $db, $ir,$userid,$c;
$q=$db->query(“SELECT count(*) FROM userstats us LEFT JOIN users u ON us.userid=u.userid
WHERE us.$mykey > $stat AND us.userid != $userid AND u.userlevel != 0”) ;
return $db->fetch_single($q);
}

Thanks for any help…

The whole line is improperly formatted

Why? It works fine in all previous versions of PHP.

Take it apart and look at what it is doing. If you remove the first set of quoted code you get this…

echo [Rank: ".get_rank($ir['strength'], 'strength')."]";

Does that look right?

Yes, although you would need the initial " after the echo. It is simply sending two values to a function and returning a value to be displayed (echoed).

It would display something like this: [Rank: 2121]

I think the mangling of the posted code is due to this ridiculous forum software, i.e. thou should not modify or directly render user submitted data. If you post any code place it inside of bbcode [ code ]…[ /code ] tags (without the spaces) before you hit the submit button.

What does var_dump of the get_rank(.,…) expression show? I would have posted the actual … line of code, but the forum software converted all the quotes to smart/curly quotes and you need to operated on your original code, not anything copied from a post here.

I hear what you are saying about the bbcode, but the line is correct as it stands - here it is between bbcode.

echo number_format($ir[‘strength’])." [Rank: ".get_rank($ir[‘strength’], ‘strength’)."]";

var_dump

Fatal error : Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) in

The formatting of the line is not the problem though as even this gives the same error:

$rank = get_rank($ir[‘strength’], ‘strength’); echo $rank;

Have you verified the $ir[‘strength’] is producing a valid value?

Also, are you using single quotes in this line? They do not match my version…

I have tried both single and double quotes in my actual coded and it makes no difference and $ir[‘strength’] is getting a value:

Strength: 996,904 [Rank: Array] - This is what is displayed on web page.

From what I have been able to figure out from reading about this error, it has something to do with the way PHP 7 and previous versions interpret the code - which is apparently different - but I can’t figure out what I would need to change. I have been passing values to functions for 25 years and never a problem until they changed php 7. This works fine in PHP 5.4 and under.

Notice : Array to string conversion in…

get_rank() is apparently returning an array, not a single value, so the problem is in your db class ->fetch_single() method. What’s the code for that?

function fetch_single($result=0) { if(!$result) { $result=$this->result; } return mysqli_fetch_assoc($result); }

Thanks

The posted code would have never produced a correct number (perhaps always a zero.) Also, it would have always been producing the Notice error, which may have been hidden by your older php version’s error_reporting, display_errors, or log_errors settings but is being displayed/logged on the current php version.

Since the fetch_single() method returns one row as an associative array, the only way for this code to have ever worked before is if you were reference the literal associative index name ‘count(*)’ in the returned array (or have used an alias name in the query and referenced the alias associative index name in the returned array.)

I figured it out and you were correct, not sure why I didn’t catch it.

I changed this: return $db->fetch_single($q);
to this: $r = $db->fetch_single($q); return $r['count(*)'];

I am not sure why it worked before but I just checked the values against my hosting site version and they come out exactly the same. Hosting site is running PHP 5.3.

Thanks to everyone for all the help.

5.3 is hundreds and hundreds of releases behind and reached end of life a long time ago. I cant believe there are still hosts running Php that old.

Sponsor our Newsletter | Privacy Policy | Terms of Service