jaymc Posted August 7, 2009 Share Posted August 7, 2009 General question.. Im making a new website and at the moment I have error reporting set to display warnings for debugging. However, when for instance I do a foreach() : if the array is empty a warning error is displaed "invalid argument supplied for foreach()" Should I be checking if the array is empty before doing a foreach even if no harm can be done either way code side I turn warning errors off once I release live so even though they are there they will never cause any issues. It just seems very tedious to me to have if (!empty($array)) {foreach().......} any time I want to do a foreach on an array that may not contain any elements Your thoughts please? p.s - I know its good practice.. but seriously.. do you bother with the example given above? Quote Link to comment https://forums.phpfreaks.com/topic/169168-fix-warnings/ Share on other sites More sharing options...
p2grace Posted August 7, 2009 Share Posted August 7, 2009 You could always force the variable to be interpreted as an array, that should fix the error. foreach((array) $array as $key => $val){ echo $val; } Otherwise, you'll need to check if the array is empty as you currently are if you want to keep you code bug free. Quote Link to comment https://forums.phpfreaks.com/topic/169168-fix-warnings/#findComment-892626 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.