PDA

View Full Version : How to Access a Variable in a While Loop


AndyL
08-11-2004, 05:57 PM
Quick Question:

Am I right in thinking that a variable/value in a While loop will cease to exist when the While loop has finished?

If this is the case how can I 'capture' a particular variable/value when iterating through the While loop and then use it OUTSIDE of the While loop?


while(list($var, $val) = each($HTTP_POST_VARS))
{
if ("Yes" == $val) // i.e. if Nil_Return == Yes
{
echo "$var: $val<BR>";
echo "Name: " . $Name . "<BR>";
echo "Date_Time_of_Completion_of_Form: " . date('d-m-Y H:i:s') . "<BR>";
break;
}
else // i.e. if Nil_Return == No
{
echo "$var: $val<BR>";
if ("Email_Address" == $var)
{
echo "Email_Address $val";
}
}
}


The line:

echo "$var: $val<BR>";

in the 'Else' statement renders the screen with a load of info and I wish to access one of those $val (e.g. "xxx@domain.com") for the respective $var (i.e. "Email_Address") and use it outside of the While loop.

I.E. what's the best way to get a variable from within a While loop for use outside of the While loop [when it's finished iterating]?

Thanks,
Andy

raf
08-11-2004, 06:31 PM
when the while loop is done, the variables just remain in the symbols table and can just be accessed. This variable will contain the value you assigned to it in the last iteration where you assigned a value to it.

if you would like to acces the variable to get the value it had for each iteration, then you just store the value in an array

but for your punctual question, this would probably the best way:

foreach($HTTP_POST_VARS as $var => $val){
echo $var . ' : ' . $val . '<br />';
if ($val=='Yes'){
echo 'Name: ' . $Name . '<br />';
echo 'Date_Time_of_Completion_of_Form: ' . date('d-m-Y H:i:s') . '<br />';
// break; do you need a brek here??
}elseif ($var == 'Email_Address'){
$email = $val
echo 'Email_Address ' . $val;
}
}
// to get the email
echo $email

AndyL
08-11-2004, 06:46 PM
Thanks for your quick reply. It's now working as a result.

Haven't used a 'foreach' before but now I see it's potential! Thank you.

BTW: I do need a 'Break;' in there so it jumps out the loop [probably 'cos I was using a While loop & I didn't want it to continue to the end after I echo to screen the 3 values if ($val=='Yes')]. Think I need a break too ... !

Thanks for your help.

Andy

dumpfi
08-11-2004, 07:13 PM
Wouldn't it be easier to get the email address by using $HTTP_POST_VARS['Email_Address'] ?

dumpfi

raf
08-11-2004, 07:21 PM
if you use a break, then the $email might not be set ...
since you can't be realy sure in what order the form-variables will be processed.

dumpfi, the $HTTP_POST_VARS['Email_Address'] would indeed be easier but i was aswering the question. Maybe something needs to be altered to it or whatever.

AndyL
08-12-2004, 10:21 AM
Thanks to both raf & dumpfi for both answering my actual question and suggesting a (better) alternative ($HTTP_POST_VARS['Email_Address']).

I used a 'break;' because I was using 'else' and not an 'elseif'. I see the benefit now though of changing to your suggested method.

Am relatively new to PHP (& mySQL) so am getting used to what it can do/how best to do it. Thanks for pointing me in the right/better direction!

Andy