PHP Tutorial (& MySQL) #12 - Continue & Break
Key Takeaways
Explains PHP continue and break keywords
Full Transcript
or rather my friends so just a short video of this time around hopefully I'm not going to be for too long but I'd like to talk about two keywords in PHP that would need to know going forward and they are break and continue and typically we use these in some kind of loops when we want to either get out of the loop or continue through the loop okay so let's talk about break first of all break is a key word in PHP that automatically allows us to break out of a loop no matter what point we are inside the loop so imagine we're cycling through these products when looping through them and we're currently iterating over this product right here if at some point during that iteration PHP comes across a break keyword it's gonna break out of the loop completely it's not gonna iterate over the last three products and it's gonna carry on with the code down here okay down the page so let's have a look at that first of all we'll do a simple example we're still cycling through all of these products and we're going to do an if tech inside and we're going to say that if the product that we're currently iterating over if the name of that product we've done quotations if that is equal to lightening bolts which is one of the products up here is this one then what we're gonna do is we're gonna break okay so we'll use the break keyword now what we're gonna do is echo down here the product name so echo and product name and then we'll echo a br as well so concatenate that with a br tag okay so what's going to happen here well we're cycling through the products and we're doing a live check on each product if the name is lightning-bolt then we're going to break at that moment in time and forget about the loop it's gonna exit out of the loop completely and no longer loop over any other products if that's not the case if this is false and we're not iterating over the product name lightning bolt then it's going to carry on and it's going to echo out the name of that product so let's save this and preview this in a browser refresh and we get undefined variable product it should be product okay save that again try second okay refresh and now we can see these first four things are printed out or echoed out to the browser and that's because when we get to the lightening bolts then this is true it breaks out the loop completely it doesn't echo the lightning bolt and we don't even iterate over this because we've broken out of the loop make sense cool now then I also want to do something else I want to say if and then in brackets product price this time if that is greater than 15 then I'm gonna use the continue keyword now what does the continue keyword do well a little bit like the break it stops the code right here and it doesn't continue with the rest of the code in the code block for that one loop but it does still go back up to the far reach and start again on the next product so for this particular product is going to continue back to the top of the loop and skip out this code but it will continue on to the next product where is the break just exits completely and doesn't continue on to the next product okay so if the price is greater than 15 for example the first one it's gonna get down here it's gonna say okay well this is true here so we're going to continue we're not going to carry on down here and execute this but we're going to jump up to the next one and we'll go to the next product okay so let's save that and preview this in a browser refresh and now we can see we've eliminated one of those products okay where the price is over 15 it was the shiny star perfect so those are the two keywords I wanted to tell you about break and continue and that's how they work inside loops
Original Description
Continue and break are two important keywords in PHP, so in this tutorial I'll show you how we can use them to break out of the normal loop cycle.
----------------------------------------
🐱💻 🐱💻 Course Links:
+ Course files - https://github.com/iamshaunjp/php-mysql-tutorial
+ VS Code editor - https://code.visualstudio.com/
+ Materialize Playlist - https://www.youtube.com/watch?v=gCZ3y6mQpW0&list=PL4cUxeGkcC9gGrbtvASEZSlFEYBnPkmff
🤑🤑 Donate
+ https://www.paypal.me/thenetninja
🎓🎓 Find me on Udemy
+ https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
🎓
Tutor Explanation
DeepCamp AI