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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:21:43+00:00 2026-05-13T18:21:43+00:00

I am new to PHP and programming in general. I have been working on

  • 0

I am new to PHP and programming in general. I have been working on a few things with PHP that have required me to create classes, which has been fine, except that I can’t seem to get my class methods to work on arrays that are properties of the class. I must be doing something pretty fundamentally wrong, because it doesn’t seem to work regardless of the project.

Here is an example I just wrote up (I thought it would be easier to post a short and simple example rather than the code of my actual project, which is kind of long and involved).

class Test {
    public $testArray;
    public $testInt;

    public function __construct() {
        $this->testArray = array();
        for ($i=1; $i<=20; $i++) {
            $this->testArray[] = array(1, 2);
        }
        $this->testInt = 4;
    }

    public function testf() {
        $this->testInt += 1;
        foreach ($this->testArray as $n) {
            $n[] = 3;
        }
    }
}

If I add some code at the end so that I get a print out of what is happening:

$obj = new Test;

echo 'Before function call:';
echo '<br />testArray:<br />';
foreach ($obj->testArray as $n) {
    print_r($n);
    echo '<br />';
}
echo '<br />testInt: ';
echo $obj->testInt;
echo '<br />';
echo '<br />';
echo '<br />';

$obj->testf();

echo 'After function call:';
echo '<br />testArray:<br />';
foreach ($obj->testArray as $n) {
    print_r($n);
    echo '<br />';
}
echo '<br />testInt: ';
echo $obj->testInt;
echo '<br />';

Then what you end up seeing is that after the function call the testInt variable changes, but the testArray doesn’t.

Please let me know if any of this isn’t clear enough. This has been driving me crazy for a while. Thank you in advance for taking the time to have a look at my problem.

  • 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-13T18:21:43+00:00Added an answer on May 13, 2026 at 6:21 pm

    When you do this:

        foreach ($this->testArray as $n) {
            $n[] = 3;
        }
    

    You’re not modifying testArray’s values. You’re working on copies of them. Any time you assign a value in PHP to another variable, you’re making a copy of the value. (Objects are sort of an exception to this, but that’s not relevant here.)

    If you want to modify the original, you’ll need to refer to it by its own variable name, like this:

        foreach ($this->testArray as $index => $value) {
            $this->testArray[$index][] = 3;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.