amg182 Posted September 19, 2011 Share Posted September 19, 2011 Apologies for such a silly question.... Been away from php for a long time... How can i call a variable from a position above where the variable is set? I need a variable to echo out near the top of my page but its defined near the bottom of my script. As you can see i am tryin to echo the amount of rows found from the query below. <?php echo $num_rows; $query = "SELECT Name, FROM table"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ $num_rows = mysql_num_rows( $result ); } ?> Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/247460-quick-question-about-calling-variables/ Share on other sites More sharing options...
requinix Posted September 19, 2011 Share Posted September 19, 2011 You can't. Quote Link to comment https://forums.phpfreaks.com/topic/247460-quick-question-about-calling-variables/#findComment-1270769 Share on other sites More sharing options...
xyph Posted September 19, 2011 Share Posted September 19, 2011 User defined functions and classes are parsed first, therefor they are available to use any any line regardless of where they are defined. This is not true when the function must be included from another file, and you try to call the function before you include the file. Variable definitions and function calls are execute top to bottom, always. Quote Link to comment https://forums.phpfreaks.com/topic/247460-quick-question-about-calling-variables/#findComment-1270774 Share on other sites More sharing options...
amg182 Posted September 19, 2011 Author Share Posted September 19, 2011 Oh I see, thanks. I will just have to move around my queries to sort my variables from top to bottom... Cheers guys. Appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/247460-quick-question-about-calling-variables/#findComment-1270781 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.