Bizty Posted August 28, 2009 Share Posted August 28, 2009 I've never really gotten the hang of the foreach() function (compared to while), but it seems that foreach() is used alot when you need to go through an array of sorts. I've now realised thats really to bad, and seeing I want to use the loop in a function I'd better get it right ^^ I have a function called CheckEmpty(), what it does is basicly checking if the supplied variable is empty. The issue is I have plenty variables (in the area of 100 for this particular script) that needs this check, so I wanted to do a simple check rather than: function CheckEmpty($var1, $var2, $var3 ..... $var99) { } so I was hoping I could do it dynamicly (maybe with an array?) where I simply go through all supplied vars. I guess it would look something like: function CheckEmpty() { foreach(something I dont get) { if(empty($var)) { return true; } else { return false; } } } Quote Link to comment https://forums.phpfreaks.com/topic/172319-solved-foreach-usage/ Share on other sites More sharing options...
ReKoNiZe Posted August 28, 2009 Share Posted August 28, 2009 Why not juse use the empty function? empty returns true and false. Quote Link to comment https://forums.phpfreaks.com/topic/172319-solved-foreach-usage/#findComment-908566 Share on other sites More sharing options...
Michdd Posted August 28, 2009 Share Posted August 28, 2009 You'd do something like this.. function checkEmpty($arr) { foreach($arr as $var) { if(empty($var)) return true; } return false; } $array = Array($var1, $var2, $var3); checkEmpty($array); Quote Link to comment https://forums.phpfreaks.com/topic/172319-solved-foreach-usage/#findComment-908567 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.