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? Quote 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 Quote 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 Quote 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 Quote 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]; } Quote 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=""; Quote 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. Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.