PHP – Conditional Statements | Loops | Functions | Arrays
Very often when you write code, you want to perform different actions for different conditions. You can use conditional statements in your code to do this.
In PHP we have the following conditional statements:
- if statement – executes some code if one condition is true
- if…else statement – executes some code if a condition is true and another code if that condition is false
- if…elseif…else statement – executes different codes for more than two conditions
- switch statement – selects one of many blocks of code to be executed.
The real power of PHP comes from its functions.
- PHP has more than 1000 built-in functions, and in addition you can create your own custom functions.
- A function is a block of statements that can be used repeatedly in a program.
- A function will not execute automatically when a page loads.
- A function will be executed by a call to the function.
An array stores multiple values in one single variable:
Often when you write code, you want the same block of code to run over and over again a certain number of times. So, instead of adding several almost equal code-lines in a script, we can use loops.
- Loops are used to execute the same block of code again and again, as long as a certain condition is true.
In PHP, we have the following loop types:
- while – loops through a block of code as long as the specified condition is true
- do…while – loops through a block of code once, and then repeats the loop as long as the specified condition is true
- for – loops through a block of code a specified number of times
- foreach – loops through a block of code for each element in an array
Who this course is for:
- Beginners to PHP
- Basic knowledge of PHP advised
Last Updated 4/2022