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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:51:02+00:00 2026-06-01T13:51:02+00:00

I would like to access class variables with for loop, here is my simple

  • 0

I would like to access class variables with for loop, here is my simple class

class test{
    public $var1 = 1;
    public $var2 = 2;
    public $var3 = 3;
    public $var4 = 4;
}


$class = new test();

this is how i try to access variables with a loop

for($i = 1; $i <= 4; $i++){
    echo $class->var.$i;
}

and i get error which says
Notice: Undefined property: test::$var in C:\xampp\htdocs\test\index.php on line 12

Well it’s not really a big error and i actualy get the value echoed but i still don’t understand why do i get this error?

also if i do it this way everything works fine:

echo $class->var1;
  • 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-01T13:51:03+00:00Added an answer on June 1, 2026 at 1:51 pm

    You’re not actually getting the value echoed, you’re getting $i echoed.

    echo $class->var.$i; is being interpreted as echo ($class->var).($i);. Since var isn’t a variable (hence the error), it becomes echo ''.$i;, so you get the value of $i. It just so happens that var1 has the value 1. (Change $var1 to something else, and you’ll see what I mean)

    To fix the issue, you can do this:

    for($i = 1; $i <= 4; $i++){
        $class->{'var'.$i}
    }
    

    The stuff inside the {} is calculated first, so the correct property is read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to access a class everywhere in my application, how can I
I have a class in my asp.net proj, I would like to get access
I would like to access the Rails session secret programmatically (I am using it
I would like to access micro-state accounting timers programmatically on Linux. I guess the
I would like to access the work items in our TFS programmatically. Shouldn't there
I would like to access information on the logical drives on my computer using
I would like to access a MySQL database using PHP. Can someone please explain
In my Servlet I would like to access the root of the context so
I'm running a wordpress blog and would like to access the actual file that
I would like to pass the reference of a variable into a class, use

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.