Perad Posted January 9, 2008 Share Posted January 9, 2008 Is there anyway I can make this loop stop searching for something once it has found its value? for($i=0; $i < count($validpages); $i++) { if ($_GET['page'] == $validpages[$i]) { $page = $validpages[$i]; } } Link to comment https://forums.phpfreaks.com/topic/85201-can-i-optimise-this-loop/ Share on other sites More sharing options...
The Little Guy Posted January 9, 2008 Share Posted January 9, 2008 <?php for($i=0; $i < count($validpages); $i++) { if ($_GET['page'] == $validpages[$i]) { $page = $validpages[$i]; break; } } ?> Link to comment https://forums.phpfreaks.com/topic/85201-can-i-optimise-this-loop/#findComment-434686 Share on other sites More sharing options...
wildteen88 Posted January 9, 2008 Share Posted January 9, 2008 if $validpages contains an array of pages and you all you want to do is see if the value of $_GET['page'] is set within that array then use in_array function instead rather than looping through the array manually. If you used in_array the above code can be written as: if(in_array($_GET['page'], $validpages) { $page = $_GET['page']; } Link to comment https://forums.phpfreaks.com/topic/85201-can-i-optimise-this-loop/#findComment-434813 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.