Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9144181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:12:24+00:00 2026-06-17T10:12:24+00:00

I am learning PHP from O’reilly media book ‘Programing PHP’ and I stumbled upon

  • 0

I am learning PHP from O’reilly media book ‘Programing PHP’ and I stumbled upon this:

function add_up ($running_total, $current_value) {
    $running_total += $current_value * $current_value;
    return $running_total;
}
$numbers = array(2, 3, 5, 7);
$total = array_reduce($numbers, 'add_up');
echo $total;

The array_reduce( ) line makes these function calls:

add_up(2,3)
add_up(11,5)
add_up(36,7)
// $total is now 87

But when I calculate I get 85. I think it should write like this:

The array_reduce( ) line makes these function calls:

add_up (0,2);
add_up (4,3);
add_up (13,5);
add_up (38,7);

Because optional value $initial is by default set to NULL.

mixed array_reduce ( array $input , callable $function [, mixed $initial = NULL ] )

Can somebody with more knowledge explain to me, who is wrong and why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:12:26+00:00Added an answer on June 17, 2026 at 10:12 am

    It has been reported in the errata (though not confirmed). But since you’re not the only one to notice, you are most likely correct.

    {128}  Section "Reducing an Array";
    Reducing An Array - Example of function calls created by array_reduce();
    
    The array reduce() line makes these function calls:
    
    add_up(2,3)
    add_up(13,5)
    add_up(38,7)
    
    The correct list of calls should be:
    
    add_up(0,2)    // This line is missing in the book
    add_up(4,3)    // This line is incorrect in the book
    add_up(13,5)
    add_up(38,7)
    
    
    [129]  first example;
    the resulting calls of the second example of array_reduce() should be:
    add_up(11, 2)
    add_up(15, 3)
    add_up(24, 5)
    add_up(49, 7)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have been learning php from this book PHP Solutions Dynamic Web Design
The code below is modified from an O'Reilly Book - Learning PHP, MySQL, and
I am learning PHP (no programming experience) from a book. The examples in the
I've been learning some PHP and MySQL from a book that teaches you how
I jut recently started learning PHP from a book called PHP/MySQL Programming for the
i was learning this PHP code from a tutorial to upload files <form method=post
Thanks for checking this out people. I'm learning PHP from PHP for Absolute Beginners
I'm developing an MVC framework in PHP from scratch; mostly for the learning experience
When learning new languages such as C++ from PHP, does reading other language snippets
Coming from PHP with some Java Struts 2.0 experience any tips/tricks on learning GWT?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.