Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code change for PHP5 2

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR

Hi guys,

I have a problem with this bit of code that works fine on PHP4 but throws a "Fatal error: Cannot use string offset as an array in ..." on PHP5.

Code:
$code_html = str_replace("<!--{banner:" . $row[0]["banner_zone_ID"] . ":" . $row[0]["banner_zone_name"] . "}-->", $banner_array["glob"][rand(0, (sizeof($banner_array["glob"]) - 1))], $code_html);

It's $banner_array that causes problem but what's wrong with it and how can I fix it?

Many thanks :)
 
Well, the error pretty much says it: one of your variables contains a string, but the code is assuming it's an array. You need to check the value of $banner_array and make sure it's actually an array. For that matter, you might want to check $row too. If it's a row from a database query, it might be that the query is coming back empty.
 
Thanks :)

Hmmm ... the code works when I remove $banner_array so I guess $row[0] is OK.

Also, I define $banner_array as an array with $banner_array = array(), so I don't see how the code could believe it's a string :(

Isn't there a quick fix to make it work on PHP5?
( I really hate when new versions make simple things harder )
 
php5 is hardly new.
i suspect the difference in behaviour is to do with a difference in your error reporting.
place this code before the echo and let us know what it reports

Code:
echo "<pre>" . print_r($row, true) . print_r($banner_array, true) . "</pre>";
 
Hi Jpadie :)

Well, PHP5 is very new ... to me :)

Anyway, I just did what you advised, and the variables return arrays as expected.

I don't get it :(
 

wew ... time for an apology :(
Deeper in the code, there was another $banner_array = "";

Problem solved after I replaced it with $banner_array = array();

Soooooryyyyyyyy :(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top