2

I'm trying to produce 25 variables from $day1Txt through to $day2Txt.

How do I turn the '1' in the variable $day1Txt into the variable $i?

for ($i = 1; $i < 25; $i++) { 

        $day1Txt = echo call_user_func('Day_'.$i.'_Offer', 'EMAIL_OFFER'); 

}

I've made an attempt myself but it white screens...

  ${'day'.$i.'Txt'} = echo call_user_func('Day_'.$i.'_Offer', 'EMAIL_OFFER'); 
4
  • 1
    Please expand on your question, what are you trying to achieve? If it is in a loop it will be keep changing until the last iteration of the loop. Commented Nov 6, 2015 at 11:19
  • 1
    Use an array instead where $i is used as index? Commented Nov 6, 2015 at 11:20
  • I just removed the echo and it hasn't errored, but I dont know if the variables are stored yet... so this probably isn't an answer. Commented Nov 6, 2015 at 11:32
  • It’s possible to get the variable name and then you could split the string (or you could hardcode the ints by using a for as you tried to), but for all sanity, you really really should use arrays instead. There is no reason not to. Commented Nov 6, 2015 at 15:16

2 Answers 2

1

This should help you(remove echo).

for ($i = 1; $i < 25; $i++) {

        ${"day".$i."Txt"} =  call_user_func('Day_' . $i . '_Offer', 'EMAIL_OFFER');
    }

If you want to see what all variables are there in your function use get_defined_vars().

See deme here

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

Comments

0

Try this

for ($i = 1; $i < 25; $i++) { 

        ${"day".$i."Txt"} = call_user_func('Day_'.$i.'_Offer', 'EMAIL_OFFER'); 

}

It's called Variable variables ie a variable name which can be set and used dynamically

2 Comments

that echo is ruining the party
@NiranjanNRaju Its Ok. Time to delete 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.