PHP Bootcamp: Part 5 – Switch, While/For Loops, And Functions
*This is the last part of our PHP bootcamp series, so if you haven’t checked out the other articles, start here!
Now that we have reached the end of our series, you should have the fundamentals in place for coding in PHP. Below is a little more detail to perfect your skills.
Switches
These are useful for when you want to execute big blocks of code that work together at the same time. An example of how this would work is if you had multiple different choices in your block, such as cars in the screenshot below:
Break is used to help keep the code from running in each case, therefore preventing excessive functions from occurring. Defaults are used if no match if found in regards to the earlier said statements. If you were to code a menu full of options for someone, then you would have an example of a switch.
While/For Loops
While Loops are statements that will continue to run as long as the condition is “true” and only stops when the condition is met by a “false” output. Loops can cause infinite loops and can be very volatile, so it is very important to emphasize “breaks” or exits paths for the While Loop to end its continuous boolean logic. While Loops need to be paired with condition as well as an exit condition, For Loops do not. These “breaks” are unique for While Loops and prevents continuous looping. If during a Loop the user selects “Esc”, this will also break the condition and the loop will exit. Infinite loops are very easy to create without much planning. Below is the syntax for a While Loop:
For Loops are different in that the objective would be if you want to repeat a line of code many times. As opposed to the While Loop, the For Loop is not conditional and has the possibility of repeating N number of times defined in the code. Below is the syntax for a For Loop:
In regards to how For Loops work, here are the parameters to keep in mind when using them:
- init counter: creates the loop counter value.
- test counter: “tested” for each loop iteration. If it comes out as true, the loop will continue. If it becomes false, the loop will end.
- increment counter: Increases the loop counter value.
Here is an example of how this would play out:
This would print out a counter from 0 to 10, stopping promptly at 10 since we have it set to stop at 10.
Functions
This is the final element in PHP and is probably the most important thing to come away with. It helps simplify code and makes things a lot easier when you’re coding a project. Essentially, this allows you to declare a word and set it as a function. Here is the syntax that you would use if you were to do so:
Keep in mind that functions are not case sensitive, and can’t start with a number. Here is a example of the code in action:
That wraps up all the fundamentals of what you need to know when it comes to getting started with coding in PHP. Hopefully you can take away some useful skills from this since being able to code is very valuable in today’s world.