Learning PHP 5 by David Sklar The unconfirmed error reports are from readers. They have not yet been approved or disproved by the author or editor and represent solely the opinion of the reader. Here's a key to the markup: [page-number]: serious technical mistake {page-number}: minor technical mistake : important language/formatting problem (page-number): language change or minor formatting problem ?page-number?: reader question or request for clarification This page was updated April 17, 2008. UNCONFIRMED errors and comments from readers: {7} Example 1-2 HTML; The tag
should be changed to
to make the example valid HTML. Alternatively, matching ending tags could be added to make the example valid XHTML. {83} Code for exercise 5-3; The tip is incremented before printing, so the total cost does not match the tip percentage listed. The line $tip++ should be moved down one line of code. [91] Example 6-7; Example 6-7 on p. 91 gives: Notice: Undefined index: _submit_check in c:\inetpub\wwwroot\6-07.php on line 2 ...because the _submit_check hidden tag is not in the document the first time (92) Example 6-8; if (array_key_exists('_submit_check', $_POST)) doesn't work; use if ($_POST['_submit_check']) as in example 6-7 instead. (94) Example 6-9; This example doesn't result in any output. (94) line just beneath this comment: //Display the form; The actual line of code is: function show_form($errors = '') { This is obvious incorrect, because it is an assignment. In this case, I believe what is missing is the negation: != meaning if there are errors the code below this line will fire telling the user the failings of their entry. {102} line 6; again in Figure 6-4; A typo and its propagation. 'fancy' used twice in each location. At each location, replace the 2nd 'fancy' with 'div' . Reason: This should just be the HTML end tag for
,
, aka.
At end of line 6: ...fancy">rice should be ...fancy">rice
In figure 6-4: ...rice & tea should be ...rice & tea [103] Ex. 6-23 (middle of page); again in Ex. 6-30, function show_form(); The examples assign $defaults = $_POST; If the magic_quotes_gpc runtime config flag is set (as it is by default), the PHP runtime automatically inserts a backslash before single and double quotes in the $_POST variables before handing them off to the page. This means in the examples, data with apostrophes or double quotes will be re-displayed to the user with backslashes if the form has errors and has to be re-shown. This input: I didn't read "War and Peace" will be re-displayed as: I didn\'t read \"War and Peace\" The book could mention two workarounds: 1. Turn off magic_quotes_gpc. 2. Assign defaults as follows: $defaults = str_replace(array('\\\'', '\\"'), array('\'', '"'), $_POST); {106} Example 6-29 -Last function listed on page; When running Example 9-17. Printing a calendar in chapter 9, both the month and year selection boxes listed the following errors: Notice: Undefined offset 1 in formhelpers.php on line 51>January (months) Notice: Undefined offset 2 in formhelpers.php on line 51>2007 (year) with the exception of the current month and year. The code for this function is below: // print out the '; } print ''; } The addition of (isset($selected_options[$option])) surrounding the array calling code determines whether the variable has been set, and does not produce the error. New code below: // print out the '; } print ''; } (130) Example 7-30; The code in the book won't produce results: // price must be a valid floating point number and // more than 0 if (floatval($_POST['price'] <= 0) { $errors[] = 'Please enter a valid price.'; } To correct add a close parenthesis after <=0) so that is reads: // price must be a valid floating point number and // more than 0 if (floatval($_POST['price'] <= 0)) { $errors[] = 'Please enter a valid price.'; } {161} first code sample; Extranous quote ini_set('session.gc_maxlifetime',600'); // 600 seconds = = ten minutes should read: ini_set('session.gc_maxlifetime',600); // 600 seconds = = ten minutes {187} both if ($_POST['_submit_check']) statements in Example 9-17. Printing a calendar.; when running Chapter 9s Example 9-17. Printing a calendar, I renamed the file "php5-cal.php" on my server, it produced the following errors: Notice: Undefined index: _submit_check in C:\webpages\Calendar_Utility\php5-cal.php on line 15 Notice: Undefined index: _submit_check in C:\webpages\Calendar_Utility\php5-cal.php on line 48 The first (line 15) code is listed below: if ($_POST['_submit_check']) { if ($errors = validate_form()) { show_form($errors); } else { show_form(); process_form(); } } else { // When nothing is submitted, show the form and then // a calendar for the current month show_form(); show_calendar(date('n'), date('Y')); } The (line 48) code is below: // If the form is submitted, get defaults from submitted variables if ($_POST['_submit_check']) { $defaults = $_POST; } else { // Otherwise, set our own defaults: the current month and year $defaults = array('year' => date('Y'), 'month' => date('n')); } The addition of (isset (**************)) into the code checks whether the variable has been set, and removes the error. Both re-written code blocks are below (line 15 first): if (isset($_POST['_submit_check'])) { if ($errors = validate_form()) { show_form($errors); } else { show_form(); process_form(); } } else { // When nothing is submitted, show the form and then // a calendar for the current month show_form(); show_calendar(date('n'), date('Y')); } // If the form is submitted, get defaults from submitted variables if (isset($_POST['_submit_check'])) { $defaults = $_POST; } else { // Otherwise, set our own defaults: the current month and year $defaults = array('year' => date('Y'), 'month' => date('n')); } {216} Figure 11-14; To get a formatted time stamp for 12/13/05 4:18:01pm, you would need to call the date() function in Example 11-16 this way: date("m/d/y g:i:sa",$timestamp); The G should be changed to g, the period after the g should be changed to a :, and you need an a after the s. {258} bottom; The executable file for CGI for PHP5 is "php-cgi.exe" so that must be entered in the third and fourth lines of the code fragment or the Apache server will not start. [283] Table B-2, Regex#6(dogsandcats..), Doesn'tMatchColumn, strings 2 and 4; The regex 'dogs? and cats?( and chickens?)?' matches 2 strings in the "Doesn't match" column. Reason: ( and chickens?)? is totally optional: the target string need only match the first part, whatever is after that match is irrelevant, it's optional because of the '(...)?', and has NO EFFECT on match or not. i.e. any string that matches just 'dogs? and cats?' is a match. Problem: The 2 strings incorrectly stated as not matching the regex, since they contain a match for the 'dogs? and cats?' portion of the regex, are a complete match. Conclusion: ( tested in GNU egrep (grep -E), on Redhat Enterprise 3) The 2 strings #2 'dogs and cats or chickens' #4 'dog and cat and chickenlegs' should be moved to the Match column from the Doesn't Match column. [711] function process_form; Example 7-56 function prcess_form line 11 if (strlen(trim($_POST['dish_name']))) { $dish = $db->quoteSmart($_POST['dish_name']); $dish = strtr($dish, array('_' => '\_', '%' => '\%')); $sql .= " AND dish_name LIKE $dish"; The last statement does NOT put the % around the variable $dish to do a LIKE search