If statements can be used to control the flow of your program depending on certain variables or user input. When using an if statement you use a comparison operator. A list of these can be found here. Below is the simplest if statement:
Code :
To help you understand a little better there is a working example below:
Code :
Output:The variable is equal to 10!
That's a very simple example and an single if statement is not often useful. However an if...else statement is extremely useful as it can direct your script in certain directions, depending on user input or variables:
Code :
Code :
Output:The variable does not equal 10!
Often within a script you need to evaluate the same variable with multiple conditions/outcomes. This is where the if..elseif..else statement comes in:
Code :
Code := 10 { echo "The variable is greater than 10"; } else { echo "You did not enter a number!"; } ?>
Output:The variable is greater than 10
However if we set our variable to "abc" this would be the result:
Code := 10 { echo "The variable is greater than 10"; } else { echo "You did not enter a number!"; } ?>
Output:You did not enter a number!
This conclude the tutorial. In the next tutorial we will learn about switch statements. These are similar to if statements but offer great flexibility!
To post comments you must sign in to your account below!
If you do not currently have an account click here to create one now!