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 6833345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:58:00+00:00 2026-05-26T22:58:00+00:00

i am using a class to make some calculations with matrices, in this case:

  • 0

i am using a class to make some calculations with matrices, in this case: Get the sum of each column.

The sum output is correct, and if I hide the notices, the problem is solved, but logically, i prefer the correction.

In PHP 5.3 I get some notices in this function:

Notice: Undefined offset: 0 
Notice: Undefined offset: 0 
Notice: Undefined offset: 1 

script

function sum()
    {
        foreach($this->numbers as $i => $rows)
        {
            foreach($rows as $j => $number)
            {
                $this->sum[0][$j] += $number; //notices here
            }
        }
        $the_sum = new matrix($this->sum, 1, $this->get_num_columns());

        return $the_sum;
    }

Matrix:

1     | 4

0.25  | 1



var_dump($this->numbers);
array
  0 => 
    array
      0 => int 1
      1 => float 4
  1 => 
    array
      0 => float 0.25
      1 => int 1

and

 $this->get_num_columns() // 2

Any idea to correct these notices ?

thanks

  • 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-05-26T22:58:01+00:00Added an answer on May 26, 2026 at 10:58 pm

    Yes, the notice occurs because there is no initial value in the variables you are adding numbers to. You should check if the number exists and initialize it before you add a number to it. (Note, this won’t improve your result, but it is good practice to initialize variables).

    function sum()
        {
            foreach($this->numbers as $i => $rows)
            {
                foreach($rows as $j => $number)
                {
                    if (!isset($this->sum[0][$j])) $this->sum[0][$j] = 0;
                    $this->sum[0][$j] += $number; //no notices here
                }
            }
            $the_sum = new matrix($this->sum, 1, $this->get_num_columns());
    
            return $the_sum;
        }
    

    Unrelated Points to Consider

    • Proper naming standards dictate that class name should be capitalized, to differentiate them from functions. So you should name your class Matrix and not matrix.
    • If you get the chance, a much better solution would be to pre-fill your array with 0s. array_fill() does the job nicely.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SharePoint 2010 I am trying to use the SPWebConfigModification class to make some
I recently had an idea to create my own String class to make using
Is okay to break all dependencies using interfaces just to make a class testable?
I was trying to make my own class for currencies using longs, but apparently
i load a class using Class.forName(klassname,false,loader) After this i create an instance using klass.newInstance();
[1] In JDBC, why should we first load drivers using Class.forName(some driver name). Why
I am using a class library which represents some of its configuration in .xml.
I'd like to use SQLiteCursor class to fetch data (not using Cursor class). This
I tried to make some sort of convenient class (below) to hold folder and
I'm trying to unit-test some classes that make use of a Singleton class whose

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.