livewirerules Posted June 28, 2010 Share Posted June 28, 2010 How do i use the values withing a while loop out side the loop? eg: $sql="select *from custwhere account='".$number."'"; $result=mysql_query($sql) or die(mysql_error()); while ($row=mysql_fetch_array($result)) { $name=$row['username']; $email=$row['email']; } I want to use the variables $name and $email outside the loop.. Ho wdo i do it? Link to comment https://forums.phpfreaks.com/topic/206032-using-variables-outside-the-loop/ Share on other sites More sharing options...
kenrbnsn Posted June 28, 2010 Share Posted June 28, 2010 You just use them. A loop is not like a function where the variable are local to the function. What's your problem? Ken Link to comment https://forums.phpfreaks.com/topic/206032-using-variables-outside-the-loop/#findComment-1078061 Share on other sites More sharing options...
livewirerules Posted June 28, 2010 Author Share Posted June 28, 2010 when i echo the variable $name out side the loop it says "Notice: Undefined variable name" and when i echo it inside the loop it works fine Link to comment https://forums.phpfreaks.com/topic/206032-using-variables-outside-the-loop/#findComment-1078063 Share on other sites More sharing options...
livewirerules Posted June 28, 2010 Author Share Posted June 28, 2010 Figured it out using an array.. works fine now Link to comment https://forums.phpfreaks.com/topic/206032-using-variables-outside-the-loop/#findComment-1078119 Share on other sites More sharing options...
Samsam Posted June 28, 2010 Share Posted June 28, 2010 print_r($name) print_r($email) or to use with echo for($i=0;$i<sizeof($name);$i++) { echo $name[$i]; } Link to comment https://forums.phpfreaks.com/topic/206032-using-variables-outside-the-loop/#findComment-1078127 Share on other sites More sharing options...
True`Logic Posted June 28, 2010 Share Posted June 28, 2010 declare the variables before the loop. $email=""; $name=""; Link to comment https://forums.phpfreaks.com/topic/206032-using-variables-outside-the-loop/#findComment-1078178 Share on other sites More sharing options...
KevinM1 Posted June 28, 2010 Share Posted June 28, 2010 declare the variables before the loop. $email=""; $name=""; That's actually defining a variable. Declaring a variable: simply writing a variable name to be used later ($var;). Defining a variable: declaring a variable and assigning a value/initializing it in one step ($var = "value"; ). Variables cannot be used unless they contain a value. Link to comment https://forums.phpfreaks.com/topic/206032-using-variables-outside-the-loop/#findComment-1078218 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.