Jump to content
Larry Ullman's Book Forums

Ch. 7. Excercise Doesn't Work, Even If Using Larry's Code


Recommended Posts

Hello,

 

I've coded up the exercises for Chapter 7, in particular, the one regarding "Larry Ullman's Books".  When I run it, I get an Array to String conversion error message.  I thought maybe I coded something wrong, so I downloaded the code from this site to double check it.  I get the same error.  Could this be a PHP version problem?  I'm using 5.5.12 with Apache version 2.4.9.

 

Thanks,

K

Link to comment
Share on other sites

  • 11 months later...

Ailime, please provide way more details:

  • What specific script are you running? (Or the specific code)
  • What version of PHP are you using?
  • What specific error message are you seeing?

Just saying the foreach loop doesn't work isn't really useful (e.g., I know it worked when I wrote the book).

Link to comment
Share on other sites

Mr. Ullman

Thank you for your quick reply. I am referring to code on chapter 7, page 166. It says you can use foreach loop to iterate through the $books array. The code for the loop is:

foreach ($books as $key => $value)
{
print "<p>$key: $value</p>\n";
}

I've checked the code several times. I even used the downloaded code. The error it throws is array to string conversion in localhost/..... .

I am using the latest apache and php version as I installed it only two weeks ago.

Link to comment
Share on other sites

You didn't say what version of PHP you're using, but I assume this issue is caused by that. If you look at my image in the book (A, on page 166), you'll see "Array" is being printed by the loop. This is because you cannot print an array by just referencing the array name. On my version of PHP when I wrote the book, PHP just printed "Array". Your version and configuration is apparently printing an error. That's no surprise. 

 

However, the first tip on page 167, and figure B, shows how you can write the loop to access each book AND each chapter.

Link to comment
Share on other sites

 Share

×
×
  • Create New...