-5

I need to create 2 variables, and 34 of each of them.

$found1 > $found34

$name1 > $name34

I tried creating them in a for loop, but they are not available outside the loop.

is there a simple way to create these globally in a php page ?

Here is the test I tried

for($i = 0; $i <= 33; $i++) {
  $found[$i] = "David".$i;
}

echo "Found 3: " . $found3;
5
  • Can you show us the code you were using? Commented Jun 10, 2016 at 1:01
  • updated sample above Commented Jun 10, 2016 at 1:04
  • 1
    Similar question 20 hours ago: stackoverflow.com/q/37716211/3933332 Commented Jun 10, 2016 at 1:05
  • Thanks, so must be an array then ? (or should be i guess?) Commented Jun 10, 2016 at 1:08
  • You just access it with the right index, like echo $found[3]; Commented Jun 10, 2016 at 1:15

1 Answer 1

0

You may need to use extract:

DEMO

for($i = 0; $i <= 33; $i++) {
  $found["found".$i] = "David".$i;
}

extract($found);

echo "Found 3: " . $found3;

Result:

Found 3: David3

So you need the key to be found.$i to get found1, found2,...


Remember: You end up creating too many variables!! It is better to use array as array

It just takes two characters to call $found3 as $found[3]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.