Jump to content
Larry Ullman's Book Forums

bvconway

Members
  • Posts

    3
  • Joined

  • Last visited

Everything posted by bvconway

  1. Ref. p.164 "Creating Multidimensional Arrays" Wondering why I keep getting "Notice"s about "Array to string conversion". I think this is covered in the book but I can't seem to find the place right now (or, I'm being lazy). I'm using Notepad v. 6.3 with XAMPP v. 3.1.0.3.1.0 with PHP v.5.4.7 and testing in Firefox (latest). Here's the "error": -------------------------------------------------------------------------------------------------------------------------- Notice: Array to string conversion in C:\xampp\htdocs\test\multidimensional_arrays\books.php on line 49 PHP VQS: Array -------------------------------------------------------------------------------------------------------------------------- I think it's covered on p.64 Error Reporting and has something to do with E_NOTICE settings. But, the program runs OK (as per the output below). I'm just wondering why it reports this "notice", or if I'm doing something wrong. Is it really performing an array-string conversion? If anything, it should non-report that I'm doing a normal, regular array-value output, where the value happens to be a string. Should I turn this "notice" off? error_reporting(0); The only slightly-odd thing I noticed while coding (using Notepadd++ v.6.3) is it keeps reporting xml:lang="en" as an unrecognized tag and colors it differently as per NPP's coloring scheme, i.e. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> Lets paste something from the PHP Manual, just for fun: Note: Enabling E_NOTICE during development has some benefits. For debugging purposes: NOTICE messages will warn you about possible bugs in your code. For example, use of unassigned values is warned. It is extremely useful to find typos and to save time for debugging. NOTICE messages will warn you about bad style. For example, $arr[item] is better to be written as $arr['item'] since PHP tries to treat "item" as constant. If it is not a constant, PHP assumes it is a string index for the array. ------------------------------------------------------------------------------------------------------------------------- My output: PHP:Groceries and Arrays - groceries.php The third chapter of my first book is HTML Forms and PHP. The first chapter of my second book is Advanced PHP Techniques The third chapter of my third book is Advanced Database Concepts Notice: Array to string conversion in C:\xampp\htdocs\test\multidimensional_arrays\books.php on line 49 PHP VQS: Array Notice: Array to string conversion in C:\xampp\htdocs\test\multidimensional_arrays\books.php on line 49 PHP Advanced VQP: Array Notice: Array to string conversion in C:\xampp\htdocs\test\multidimensional_arrays\books.php on line 49 PHP and MySQL VQP: Array ---------------------------------------------------------------------------------------------------------------------------------------------- Here is the full code I used: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>PHP:Multidimensional Arrays</title> </head> <body> <h2>PHP:Groceries and Arrays - groceries.php</h2> <br /> <?php $phpvqs = array( 1 => 'Getting Started with PHP', 'Variables', 'HTML Forms and PHP', 'Using Numbers'); //uses no.s for keys and strings for values //no.1 above refers to chapter 1 $phpadv = array( 1 => 'Advanced PHP Techniques', 'Developing Web Applications', 'Advanced Database Concepts', 'Security Techniques'); $phpmysql = array( 1 => 'Introduction to PHP', 'Programming with PHP', 'Creating Dynamic Web Sites', 'Introduction to MySQL'); $books = array ( 'PHP VQS' => $phpvqs, 'PHP Advanced VQP' => $phpadv, 'PHP and MySQL VQP' => $phpmysql ); //uses strings for keys and arrays for values //print out the name of the third chapter of the PHP //Visual QuickStart Guide //print "{$books['PHP VQS'][2]}"; //above line is wrong - 3rd chap is at index 3 print "<p>The third chapter of my first book is <i>{$books['PHP VQS'][3]}.</i></p>"; print "<p>The first chapter of my second book is <i>{$books['PHP Advanced VQP'][1]}</i></p>"; print "<p>The third chapter of my third book is <i>{$books['PHP Advanced VQP'][3]}</i></p>"; foreach ($books as $key => $value) { print "<p>$key: $value</p>\n"; } //the $key variable gets each abbreviated book title //the $value variable contains each chapter-array //compare to //$list = array( //1 => 'apples', //2 => 'bananas', //3 => 'oranges' //); //arrays automatically begin their indexing at 0, unless otherwise specified. You can assign the index when using array() as above ?> <pre> <?php print_r ($books); var_dump($books); foreach ($books as $title => $chapters) { print "<p>$title"; foreach ($chapters as $number => $chapter) { print "<br />Chapter $number is $chapter"; } print "</p>"; } ?> </pre> </body> </html> <pre style="visibility:hidden"> Comments: <!-- Multidimensional arrays: - you use arrays as elements instead of strings or numbers ------------------------------------------------------------------- --> </pre>
  2. Just want to say what an excellent book this is, i.e. "PHP for the Web" [4th edition]. I very much like the writing, the pace of instruction, the layout and everything else. I had minor problems in spacing because of the narrow layout columns that force code onto the next line - couldn't see if two characters were right next to each other or if there was actually a space between them. The little grey arrows on the left side of the code could be either a space or a hard return, no? It actually hasn't caused me any real problems, though, since I can deduce from context whether I need a space or a hard return. Curious thing, your images just to right of, e.g. the "#3" etc.on this page : <img src="http://www.larryullman.com/forums/public/style_images/larry_ullman/icon_share.png" class="small" title="Review And Pursue: post #5"> are not showing up in Chrome (latest version as of Feb 11, 2013). They show up in Firefox ok. Also, what would be a good book - of yours - to follow this one? That is, to take me to the next level of PHP programming. I shall check the book list ... Many thanks, Bruce Conway
×
×
  • Create New...