Jump to content

[SOLVED] PHP Strings


spookykl

Recommended Posts

Hi all,

 

I am fairly new with php coding and this may seem like a stupid question, but how do I (using a for loop), loop through each of my variables? (look at the example below).  I'm not sure how to explain it, but it goes something like this:

 

$TXT1 = "Hello";

$TXT2 = "My Name Is";

$TXT3 = "PHPFREAKS";

$TXT4 = "ByeBye";

 

for ($i = 1; $i<=4; $i++)

{

  $output .= $TXT . $i;  <--  how can I combine this and make it $TXT1 or $TXT2, etc...

}

 

Thank you!

Link to comment
Share on other sites

do some reading cause these things called arrays are amazing:

 

http://us.php.net/manual/en/ref.array.php

 

Loop structures:

http://us.php.net/manual/en/control-structures.while.php

http://us.php.net/manual/en/control-structures.for.php

 

http://us.php.net/manual/en/control-structures.foreach.php *

 

foreach is a very powerful tool for running through an array.

 

 

Edit:

with arrays you can accomplish that in a cleaner way

<?php
$txt = array();
$txt[1] = "Hello";
$txt[2] = " My Name Is";
$txt[3] = " PHPFREAKS";
$txt[4] = " ByeBye.";
foreach($txt as $value){
echo $value;
}
?>

No need to know your array's size

Link to comment
Share on other sites

If you have a list of sequentially named variables, it is a sure bet that you should be using an array.

 

A list of sequentially named variables will require you to carry around another variable that says how many of them there are. With an array, you don't need to waste time and code doing that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.