Search the Community
Showing results for tags 'error reporting'.
-
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>
- 3 replies
-
- array
- error reporting
-
(and 3 more)
Tagged with: