Errata

PHP Cookbook

Errata for PHP Cookbook

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released.

The following errata were submitted by our customers and have not yet been approved or disproved by the author or editor. They solely represent the opinion of the customer.

Color Key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted by Date submitted
PDF, Mobi Page 4
Last paragraph

In line
Nowdocs are similar to heredocs, but the is no
Should say there is no

Angel Gonzalo  Feb 14, 2014 
PDF, Mobi Page 4
Last example

In the example the nowdocs command is not written

Here

For example, if you?re using jQuery:
$.ajax({
url: "/api/getStock",
data: {

I would add

For example, if you?re using jQuery:
<?php
$str = <<< 'EOD'
$.ajax({
url: "/api/getStock",
data: {
...
EOD;

I also find that the explanation is in process as it's a copypaste from php.net manual

Angel Gonzalo  Feb 14, 2014 
Printed Page 42
top of the page function defined as squares


Function names squares returns empty array when start is lower than stop.

TEST FAILURE

function testSquares()
{
$result = [4 => 16, 3 => 9, 2 => 4, 1 => 1];
$this->assertEquals($result, makeSquares(4,1));
}


function makeSquares($start=1, $stop=1)
{
$result = [];

foreach (squares($start, $stop) as $number => $square) {
$result[$number] = $square;
}

return $result;
}

aju  Nov 12, 2018 
Printed Page 42
Beginning paragraph

function squares($start, $stop) {
if ($start < $stop) {
for ($i = $start; $i <= $stop; $i++) {
yield $i => $i * $i;
}
}
else {
for ($i = $stop (should be $start); $i >= $start (should be $stop); $i--) {
yield $i => $i * $i;
}
}
}
foreach (squares(3, 15) as $n => $square) {
printf("%d squared is %d\n", $n, $square);
}

Anonymous  Dec 29, 2018 
Printed Page 116
First Discussion section - top of page

Finding the maximum value using the arsort() function will NOT work since the array is not reindexed.
Therefore, the $array[0] value will not be the maximum with arsort($array);

Must use rsort($array) to get the maximum value in $array[0].

Anonymous  Jun 24, 2017 
Printed Page 118
First Discussion section - top of page

[Sorry - I typed the wrong page number. Not page 116 but page 118]

Finding the maximum value using the arsort() function will NOT work since the array is not reindexed.
Therefore, the $array[0] value will not be the maximum with arsort($array);

Must use rsort($array) to get the maximum value in $array[0].

Anonymous  Jun 24, 2017 
Printed Page 140
Discussion section middle of page

"Using isset() is essential when assigning default values. Without it, the nondefault value can't be 0 or anything else that evaluates to false. Consider this assignment:

$cars = isset($_GET['cars']) ? $_GET['cars'] : $default_cars;

If $_GET['cars'] is 0, $cars is set to $default_cars even though 0 may be a valid value for $cars."

WRONG!!!! $cars in the above code will be set to 0 if $_GET['cars'] is 0!!!!

The example meant to say this:

"Consider this assignment:

$cars = $_GET['cars'] ? $_GET['cars'] : $default_cars;
[...]"

Because we see the author trying to make the point that using isset() is needed here to prevent the logic error in his example.

Anonymous  Jun 25, 2017 
Printed Page 183
class definition top half of page

The class guest_book does not have $comments declared as an array. In fact, $comments is not declared at all.

My version of Php will not properly initialize the $comments array when

array_unshift($this->comments, $comment);

is executed in the class.

When I added:

public $comments = []; to the top of the class the code worked.

Anonymous  Jun 26, 2017 
Printed Page 183
Additional comment on previous post

Of course we all know PHP does not require variables to be declared prior to use.

In my previous errata post, I am indicating that the intent of the author was to predeclare variables (see example $last_visitor), and also that using array_unshift on a NON-ARRAY is a problem for PHP. As in, it does not work.

Anonymous  Jun 26, 2017 
Printed Page 213
First line

It would be more accurate to say:

"Prefix parent:: to the method name"

instead of:

"Prefix parent to the method name"

Anonymous  Jun 26, 2017 
Printed Page 242
Second Paragraph

The author forgot to update the example URL:

"Consider the partial URL for retrieving information about a stereo system:

/stereo.php?speakers=12&cdplayer=52&10

The HTML entity for an ampersand (&) is &amp; so a browser may interpret that URL as:

/stereo.php?speakers=12&cdplayer=52&10"

Author just repeats the same URL! The URL is valid as is.

Anonymous  Jun 25, 2017 
PDF Page 261
Sending a specific HTTP Status Code

The solution shows a wrong function call.

expected: http_response_code( )
written: header('Location: ...

Anonymous  Jan 23, 2014 
Printed Page 261
First paragraph top of page

Code at top of page of Solution missing a line:

$browser = get_browser(); // <= Missing this line

if ($browser->ismobilebrowser) {
// print mobile layout
} else {
// print desktop layout
}

Anonymous  Jun 25, 2017 
Printed Page 265
Code line 35 (on page, line 62 over all)

<code>
function pageheader($page) ?>
</code>

should be

<code>
function pageHeader($page) ?>
</code>

WSaewyc  Oct 06, 2017 
PDF Page 396
Parsing Large Xml Documents (general remark)

Sorry, I did not know how to contact the authors besides an errata - so this is not an errata but a suggestion for an improval for the section "Parsing Large Xml Documents".

You may want to add a method which combines the XMLReader (large files) with the SimpleXMLElement (easy access).

This is by far the best solution to deal with large XML files in PHP I am aware of.

For an example see the first answer/comment in this post http://stackoverflow.com/questions/1835177/how-to-use-xmlreader-in-php

Best
Johannes

Anonymous  Jan 23, 2014 
Printed Page 547
Second Paragraph

Book states:

"By default, PHP accepts a session identifier sent in a cookie, but if session.use_only_cookies is set to 1, it will accept a session identifier in the URL."

WRONG!!!!
If session.use_only_cookies is set to 0, it will accept a session id in the URL. If it is set to 1, it will only accept cookies!

The book has it exactly backwards.

Anonymous  Jun 25, 2017 
Mobi Page 3930
Location is kindle location rather than page number

The sentence reads "When you call mean(96,93,98,98), func_num_args() returns 3. The first argument is in position 0, so you iterate from 0 to 2, not 1 to 3."

There are four arguments passed in to the function, so it should read "When you call mean(96,93,98,98), func_num_args() returns 4. The first argument is in position 0, so you iterate from 0 to 3, not 1 to 4."

David Wilkie  Mar 06, 2014 
Mobi Page 15931
Kindle Location (not page number)

Numerous links to other recipes in the book are missing. Here are a few Kindle locations where these links are missing:
15931
17858
21028
24385
24390
24391

Anonymous  Dec 23, 2015