Join David Powers for an in-depth discussion in this video Alternative syntax for loops, part of Learning PHP.
- Normally, the body of a loop is enclosed in curly braces,…however, there is an alternative syntax that can be used…with while, for, and foreach loops.…It can be useful if you want to…make it clear where a loop ends.…This is while.php, which has a while loop.…With the while loop, you replace the opening curly brace…with a colon,…and you replace the closing curly brace…with the keyword endwhile.…
This is all one word.…There must be no space between the end and while.…And it needs to be followed by a semicolon.…We save that, and load this page into a browser,…it goes through that loop in exactly the same way…as if we had used curly braces.…So let's take a look at a for loop.…That's in for_loop.php.…It follows exactly the same pattern.…You replace the opening curly brace with a colon,…and then because we're dealing with a for loop,…the closing curly brace is replaced by…the keyword endfor, and that's followed by a semicolon.…
If we save that, and load this into a browser,…it loops through the array as before.…So let's take a look at foreach,…
Author
Released
11/20/2015- Naming variables
- Storing text as strings
- Doing calculations with PHP
- Using conditional statements to make decisions
- Creating custom functions
- Deciphering error messages
- Emailing the contents of an online form
- Dealing with multiple-choice form fields
Skill Level Beginner
Duration
Views
Related Courses
-
Teach Kids Programming with PHP
with Mark Niemann-Ross1h 39m Beginner
-
Introduction
-
Welcome1m 11s
-
Using the exercise files4m 14s
-
-
1. Checking Your Setup
-
2. PHP Basics
-
PHP: The big picture6m 39s
-
Comments and white space6m 7s
-
Joining strings9m 4s
-
-
3. Making Decisions with Conditional Statements
-
The truth according to PHP3m 21s
-
Setting a default value8m 52s
-
-
4. Doing Calculations with PHP
-
Arithmetic operators9m 15s
-
Challenge: Integer division1m 18s
-
Solution: Integer division3m 27s
-
-
5. Using Loops for Repetitive Actions
-
Using a for loop5m 44s
-
Alternative syntax for loops2m 32s
-
6. Using Functions and Objects
-
Creating custom functions7m 53s
-
7. Including External Files
-
Using server-side includes3m 32s
-
Using include and require8m 16s
-
Examining the include_path5m 12s
-
-
8. Handling Errors
-
Dealing with PHP errors1m 27s
-
-
9. Emailing the Contents of an Online Form
-
Blocking suspect phrases8m 2s
-
Preparing to send email5m 10s
-
Automating the reply address7m 46s
-
Building the message body10m 18s
-
Sending the email5m 7s
-
Troubleshooting mail()3m 17s
-
10. Dealing with Multiple-Choice Form Fields
-
Introduction1m 58s
-
Handling checkbox groups7m 26s
-
Handling select menus6m 21s
-
-
Conclusion
-
Where next?1m 56s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Alternative syntax for loops