👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
Examples :Input: array1 = array (1, 2, 3, 4, 5, 6, 7) Output: 1 2 3 4 Loop Terminated The loop contains an if condition and when condition is true then loop will break otherwise display the array content. Input: array1 = array (’10’,’ 2’, ’5’,’ 20’, ’40’) Output: 10 2 Loop TerminatedProgram:
// PHP program to break the loop
// Declare an array and initialize it
$array
=
array
(1, 2, 3, 4, 5, 6, 7);
// Using a foreach loop
foreach
(
$array
as
$a
) {
if
(
$a
== 5)
break
;
else
echo
$a
.
""
;
}
echo
" "
;
echo
"Loop Terminated"
;
?>
Exit:1 2 3 4 Loop Terminated
Method 2:given nested loops, in PHP we can use break 2, to also terminate two loops. At the bottom, the program contains a nested loop and ends it with a break statement.
For example, given two arrays arr1 and arr2, the challenge is to map all arr2 values ‚Äã‚Äãfor each arr1 value to arr1 that is not equal to arr2. If the value in arr1 is equal to the value of arr2, terminate both loops using break 2 and continue with statements.Examples : Input: arr1 = array (’A’,’ B’, ’C’); arr2 = array (’C’,’ A’, ’B’,’ D’); Output: AC Loop Terminated Input: arr1 = array (10, 2, 5, 20, 40) arr2 = array (1, 2) Output: 10 1 2 2 1 Loop Terminated
// PHP program to break the loop
// Declare two arrays and initialize it
$arr1
=
array
(
’ A’
,
’B’
,
’C’
);
$arr2
=
array
(
’C’
,
’A’
,
’ B’
,
’D’
);
// Using a foreach loop
foreach
(
$arr1
as
$a
) {
echo
"$a"
;
// Ue nested loop
foreach
(
$arr2
as
$b
) {
if
(
$a
! =
$b
)
echo
"$b"
;
else
break
2;
}
echo
" "
;
}
echo
"Loop Terminated"
;
?>
Exit:AC Loop Terminated
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from PHP break (single and nested loops), check other array Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano PHP break (single and nested loops)
- Deutsch PHP break (single and nested loops)
- Français PHP break (single and nested loops)
- Español PHP break (single and nested loops)
- Türk PHP break (single and nested loops)
- Русский PHP break (single and nested loops)
- Português PHP break (single and nested loops)
- Polski PHP break (single and nested loops)
- Nederlandse PHP break (single and nested loops)
- 中文 PHP break (single and nested loops)
- 한국어 PHP break (single and nested loops)
- 日本語 PHP break (single and nested loops)
- हिन्दी PHP break (single and nested loops)
Walter Porretti
Paris | 2023-03-22
Simply put and clear. Thank you for sharing. PHP break (single and nested loops) and other issues with re Python module was always my weak point 😁. Will get back tomorrow with feedback
Davies Lehnman
California | 2023-03-22
Thanks for explaining! I was stuck with PHP break (single and nested loops) for some hours, finally got it done 🤗. Checked yesterday, it works!
Schneider Sikorski
Munchen | 2023-03-22
re Python module is always a bit confusing 😭 PHP break (single and nested loops) is not the only problem I encountered. Will use it in my bachelor thesis