mga_ka_php Posted September 24, 2010 Share Posted September 24, 2010 why does wordpress is using foreach rather than while in listing records? common while ($products = mysql_fetch_object($rs) { ... } but wordpress is using foreach foreach ($posts as $post) { ... } which is better? because i'm trying to create my own code for database connection. Quote Link to comment https://forums.phpfreaks.com/topic/214266-while-or-foreach/ Share on other sites More sharing options...
Chris92 Posted September 24, 2010 Share Posted September 24, 2010 foreach is faster. Quote Link to comment https://forums.phpfreaks.com/topic/214266-while-or-foreach/#findComment-1114938 Share on other sites More sharing options...
mga_ka_php Posted September 24, 2010 Author Share Posted September 24, 2010 i see that they use array in getting records from the database. while ( $row = @mysql_fetch_object( $this->result ) ) { $this->last_result[$num_rows] = $row; $num_rows++; } what if my records is 50,000. does array can hold that number of records, or does it use to much memory? Quote Link to comment https://forums.phpfreaks.com/topic/214266-while-or-foreach/#findComment-1114941 Share on other sites More sharing options...
Chris92 Posted September 24, 2010 Share Posted September 24, 2010 Depending on your computer and PHP settings. It might timeout if your computer can't store that much. It's better to then unset the records after they have been used. Quote Link to comment https://forums.phpfreaks.com/topic/214266-while-or-foreach/#findComment-1114945 Share on other sites More sharing options...
rwwd Posted September 24, 2010 Share Posted September 24, 2010 Is this wordpress too:- while ( $row = @mysql_fetch_object( $this->result ) ) { Bad coding methods afoot here... If it is, this is why I use a tiny text editor, so that I know what's going on, and don't get me started on DreamWeaver, ugh. Original question: While loops are preferred for iterating through DB/Array records, for loops and foreach take more memory as you are assigning temporary memory whilst the loop completes, also, preferred to use functions OUTSIDE the for loop to then there is less over head involved parser side. Rw Quote Link to comment https://forums.phpfreaks.com/topic/214266-while-or-foreach/#findComment-1114949 Share on other sites More sharing options...
mga_ka_php Posted September 24, 2010 Author Share Posted September 24, 2010 while loop is better? Quote Link to comment https://forums.phpfreaks.com/topic/214266-while-or-foreach/#findComment-1114957 Share on other sites More sharing options...
salathe Posted September 24, 2010 Share Posted September 24, 2010 Neither is better nor worse. They're just two ways of looping. Choose whichever you find gets the job done best, or at all. Quote Link to comment https://forums.phpfreaks.com/topic/214266-while-or-foreach/#findComment-1114969 Share on other sites More sharing options...
kickstart Posted September 24, 2010 Share Posted September 24, 2010 Hi Using foreach for this suggests that Wordpress is reading the entire result set into an array before processing it, rather than allowing the programmer to just retrieve a row at a time. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/214266-while-or-foreach/#findComment-1114977 Share on other sites More sharing options...
mga_ka_php Posted September 24, 2010 Author Share Posted September 24, 2010 thanks for all of your replies. Quote Link to comment https://forums.phpfreaks.com/topic/214266-while-or-foreach/#findComment-1114998 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.