Jump to content

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?
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
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.
<?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>';
}

?>
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!
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
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.