Free cookie consent management tool by TermsFeed Generator PHP break and continue | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP break and continue

PHP break and continue

Last Updated on Mar 21, 2023

We have already talked about loops. Inside the loop we can use 2 words

  • break
  • continue

Let’s see what they mean and what they do.

Break

We are telling php to jump out of the loop completely and execute the rest of the code.

For example we're looking for a value inside an array. we'll check each value inside the array to see if it is what we’re looking for. When we find our value we don’t care about the rest. so we want to jump completely out of the loop and execute the rest of the code.

<?php
$names = ['Vitto','Simon','Pratham','Hassib','Thomas','Tom'];

foreach($names as $name){
    if($name == 'Pratham'){
        // found him. there is no need to continue
        break;
    }
}
// execute the rest of the code

Continue

We are telling php to skip the current iteration of the loop and execute the next one.

For example we want to echo some of the values inside an array. So if the value doesn’t have what we specified, we'll to go to the next iteration. We don’t want to completely end the loop.

$numbers = [50,250,5421,3,756,93,43];

foreach($numbers as $number){
    // skip the numbers that are divisible by 3
    if($number % 3 == 0){
        continue;
    }
    echo $number;
}

https://youtu.be/F8Z31bQ-1x8

Conclusion

Now you know about break and continue in PHP.

I recommend you to open a PHP files and write a loop. try the words continue and break and see what happens.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • continue in a loop PHP
  • break in a look PHP

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Inheritance
Mar 22, 2023 programming

PHP Inheritance

Today we are going to talk about Inheritance in PHP. Inheritance is another important topic related to object oriented programming in PHP. It can help you extend the functionalities of a class. ...

8 Min Read Read More
PHP simple REST API
Feb 15, 2023 programming

PHP simple REST API

Today we are going to create a simple REST API in PHP. we are going to do it with pure PHP and with no frameworks. you can create a RESTful API in 70 lines of code. ...

25 Min Read Read More
PHP Interacting with Python
Mar 22, 2023 programming

PHP Interacting with Python

Today we are going to talk about interacting with a python file in PHP. Both PHP and Python are very powerful languages. you can run a python program from your PHP code. ...

6 Min Read Read More
PHP Array Search
Feb 14, 2023 programming

PHP Array Search

Today we are going to talk about array search function in PHP. array search is a very useful function that helps you find the key of a value you’re looking for. ...

3 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...