Jump to content

Archived

This topic is now archived and is closed to further replies.

pakenney38

setting vars with recurring numbers within the vars

Recommended Posts

For example, I got this to work:

[code]<?PHP

for ($counter = 1; $counter <= 200; $counter++)
{
  $T[$counter] = 'T' . $counter;
  echo $T[$counter] . '<br>';
}

if ($T[1])
{
echo 'yes<br>';
}

else
{
echo 'no<br>';
}

if ($T[2])
{
echo 'yes<br>';
}

else
{
echo 'no<br>';
}

?>[/code]

But what if I didn't want my variables to come out named $T[1], $T[2], $T[3]....etc?
What if I wanted $T1, $T2, $T3....etc?
How do I do this without setting each variable $T1 through $T200 independently?

Share this post


Link to post
Share on other sites
Why would you want individual variables instead of an array? Arrays are so much easier to use.

BTW, please change the tags around your code from [nobbc][quote][/quote][/nobbc] to [nobbc][code][/code][/nobbc]

Ken

Share this post


Link to post
Share on other sites
Well, for this project I am dealing with some 220 HTML form fields whose values must find their way to a MySQL database. For the sake of my sanity, I am trying to keep the form fields the same name as the PHP variables and the MySQL fields. Also, time is the primary concern for this project, as are all of my projects at work, so generally while coding I like any amount of code that I can copy and paste, even if it's just field names.

Share this post


Link to post
Share on other sites
<?PHP

for ($counter = 1; $counter <= 200; $counter++)
{
  $T[$counter] = 'T' . $counter;
  echo $T[$counter] . '<br>';
}

$t1=$T[1];
$t2=$T[2];

if ($t1)
{
echo 'yes<br>';
}

else
{
echo 'no<br>';
}

if ($t2)
{
echo 'yes<br>';
}

else
{
echo 'no<br>';
}

?>

Share this post


Link to post
Share on other sites
for the

$t1 = $t[1];
ect....

instead of typeing all those out couldnt you just do a loop.  ive been studing php for about a week now so i couldnt really type an example but ill try  ;)

for ($i = 1; $i <=200; $i++)
{
$t.$i = $t[.$i.];
}

looks like it would work..  i could be way off though..  HAH!

Share this post


Link to post
Share on other sites
Before posting code that "looks right," please test it first to see if it is right.

Your code:
[code]<?php
for ($i = 1; $i <=200; $i++)
{
$t.$i = $t[.$i.];
}
?>[/code]
Is incorrect. The correct way of doing this is:
[code]<?php
for ($i = 1; $i <=200; $i++)
{
${t.$i} = $t[$i];
}
?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

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.