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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:16:58+00:00 2026-06-04T01:16:58+00:00

I want to access a variable that is either called $item1 , $item2 or

  • 0

I want to access a variable that is either called $item1, $item2 or $item3.

I want to access this variable inside a for loop where $i is ++ every time. using $item.$i or something similar. However using that code means that I am trying to join the contents of two variables, and there is no variable called $item.

  • 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-04T01:16:59+00:00Added an answer on June 4, 2026 at 1:16 am

    Arrays: A Better Method

    While PHP does permit you to build dynamic variable names from various other values, you probably shouldn’t in this case. It seems to me that an array would be more appropriate for you:

    $items = array( 0, 12, 34 );
    

    You could then access each value individually:

    echo $items[0]; // 0
    echo $items[1]; // 12
    

    Or loop over the entire set:

    foreach ( $items as $number ) {
      echo $number; // 1st: 0, 2nd: 12, 3rd: 34
    }
    

    Merging Multiple Arrays

    You indicated in the comments on the OP that $item1 through $item3 are already arrays. You could merge them all together into one array if you like with array_merge(), demonstrated below:

    $item1 = array( 1, 2 );
    $item2 = array( 3, 4 );
    $item3 = array( 5, 6 );
    
    $newAr = array_merge( $item1, $item2, $item3 );
    
    print_r( $newAr );
    

    Which outputs:

    Array
    (
      [0] => 1
      [1] => 2
      [2] => 3
      [3] => 4
      [4] => 5
      [5] => 6
    )
    

    If You Must: Dynamic Variable Names

    For completeness, if you were to solve your problem by dynamically constructing variable names, you could do the following:

    $item1 = 12;
    $item2 = 23;
    $item3 = 42;
    
    for ( $i = 1; $i <= 3; $i++ ) {
      echo ${"item".$i} . PHP_EOL;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a function(initialize) inside the a.as File.i want to access the that Variable
I want to define a class method that has access to a local variable.
I want the value of JavaScript variable which i could access using PHP. I
I want to set a variable in index.php and want to access that variable
I want to, from JavaScript, access as a variable the file that is loaded
I want to have a variable that I can access anywhere by importing a
I want to access the value of a specific variable (in the url) in
I want to defined a variable/parameter in CSS and access it by javascript. What
I want to access a repository using both GIT and SVN clients. A way
I want to access the clipboard using Python 3.1. I've obviously come across win32clipboard,

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.