1

I'm a Flash (AS2) programmer trying to do the following in PHP:

while (var i=1;i<6;i++){
   echo descriptionTextForDay[i]
}

where descriptionTextForDay1 - descriptionTextForDay5 are previously defined strings, not members of an array.

The code above is how I'd have done it in AS2 (except for the echo), but how would you do this in PHP?

Sorry if it's a basic question, but I've passed my eyes through the documentation without finding any answers.

5
  • Please review the language reference section. Please. Commented Dec 12, 2012 at 4:56
  • I did - I think you're missing what I'm asking about. I know that you'd use a "for" loop etc. and I know you'd need semicolons in php, that you don't use "var" and so on - but I couldn't find anything on how [] is used (outside of arrays) in php. (which is the only thing I actually needed help with) Commented Dec 12, 2012 at 5:08
  • You might want to go back and read it again. You'd have come across variable variables, which are actually closer to what AS2 does than using an array instead. They're also a horrible idea and shouldn't be used unless you have no other choice. I suppose, then, that it all worked out for the best! Commented Dec 12, 2012 at 5:11
  • Well, obviously the answer is in there, and obviously I missed it - I'm not saying the documentation is incomplete. But, it was way faster for me to ask about this one particular thing rather then going through the entire reference with a magnifying glass looking for something I didn't even know the terminology for. (thanks for letting me know they're called "variable variables") Commented Dec 12, 2012 at 5:19
  • And now, you'll never forget! Just make sure to never use them until you hate...er.. understand their downfalls. Commented Dec 12, 2012 at 5:22

3 Answers 3

4

You would use a for loop, like this:

for( $i = 1; $i < 6; $i++) {
    echo $descriptionTextForDay[$i];
}

Also:

  1. Variables start with a dollar sign.
  2. The var keyword is not recommended for use (and not used in this context).
  3. Statements end with a semicolon.
Sign up to request clarification or add additional context in comments.

Comments

2

Use foreach!

foreach($descriptionTextForDay as $index => $content) {
  echo "$index is $content";
}

$index is optional, you may also do:

foreach($descriptionTextForDay as $content) {
  echo $content;
}

The former is more useful if you declare an array that has useful keys, like:

$myArray = array(
 'timmy' => 'bad',
 'jimmy' => 'good',
);

foreach($myArray as $boy => $status) {
  echo "$boy is $status\n";
}

// Output:
// timmy is bad
// jimmy is good

Good luck transitioning!

Comments

1

try this..

$i = 0;
 while($i<6){
   echo $descriptionTextForDay[$i];
   $i++;
  }

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.