break out of if and foreach

if is not a loop structure, so you cannot “break out of it”. You can, however, break out of the foreach by simply calling break. In your example it has the desired effect: $device = “wanted”; foreach($equipxml as $equip) { $current_device = $equip->xpath(“name”); if ( $current_device[0] == $device ) { // found a match in … Read more

Iterating over result of getElementsByClassName using Array.forEach

No, it’s not an array. As specified in DOM4, it’s an HTMLCollection (in modern browsers, at least. Older browsers returned a NodeList). In all modern browsers (pretty much anything other IE <= 8), you can call Array’s forEach method, passing it the list of elements (be it HTMLCollection or NodeList) as the this value: var … Read more

“continue” in cursor.forEach()

Each iteration of the forEach() will call the function that you have supplied. To stop further processing within any given iteration (and continue with the next item) you just have to return from the function at the appropriate point: elementsCollection.forEach(function(element){ if (!element.shouldBeProcessed) return; // stop processing this iteration // This part will be avoided if … Read more

Invalid argument supplied for foreach()

Personally I find this to be the most clean – not sure if it’s the most efficient, mind! if (is_array($values) || is_object($values)) { foreach ($values as $value) { … } } The reason for my preference is it doesn’t allocate an empty array when you’ve got nothing to begin with anyway.

Break or return from Java 8 stream forEach?

If you need this, you shouldn’t use forEach, but one of the other methods available on streams; which one, depends on what your goal is. For example, if the goal of this loop is to find the first element which matches some predicate: Optional<SomeObject> result = someObjects.stream().filter(obj -> some_condition_met).findFirst(); (Note: This will not iterate the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)