The condition itself can be anything you need or chose and you can build (combined) multiple conditions to get the thing working the way you want it.
For example:
- $The_age = 20;
- if ($The_age < 18) {
- print "You are very young young - under 20!\n";
- } elseif ($The_age > 18) {
- print "You're not under 18 - not so very young:) !\n";
- }
- if ($The_age >= 18 && $The_age < 50) {
- print "You're in the prime of your life\n";
- } else {
- print "You're not in the prime of your life\n";
- }
- if ($The_age >= 50) {
- print "You can retire soon - hurrah!\n";
- } else {
- print "You cannot retire soon :( ";
- }
Mostly, PHP evaluates the statements (if statements) from left to right, so it forst checks if $The_age is greater or equal to 18 (our fixed line in the example) and then it will check if the same value is $The_age is less than 50. When you are combining to statements with a double ampersand, &&, it means that both of them must be true so that the code could print out "You're in the prime of your life\n"; otherwise it will print "You're not in the prime of your life\n";
In the same way (to combine statements) as using the double ampersand we can use the pipe symbol printed twice (||) which means OR and in this case, if any of the two statements we have defined is true, it will print out "You're in the prime of your life\n";.
&& = AND - both of our statements must be true for the code to be executed
|| = OR - at least on of our statements must be true for the code to be executed.
There are several ways to compare two numbers. We have just looked at < (less than), <= (less than or equal to), and >= (greater than or equal to). We will be looking at the complete list later, but first I want to mention one important check: = =, or two equals signs put together. That means "is equal to." Therefore 1 == 1 is true, and 1 == 2 is false.
The code to be executed if the statement is true is in its own block (remember, a block starts with { and finishes with }), and the code to be executed otherwise is in an else block. This stops PHP from trying to execute both the true and false actions.
One key thing to note is that PHP practices "if statement short-circuiting"this is where PHP will try to do as little conditional work as possible, so it basically stops checking conditional statements as long as it is sure it can stop. For example:
- if ($Age > 10 && $Age < 20)
If $Age evaluates to 8, the first check ($Age > 10) will fail, so PHP will not bother checking it against 20. This means you can, for example, check whether a variable is set and whether it is set to a certain valueif the variable is not set, PHP will short-circuit the if statement and not check its value. This is good because if you check the value of an unset variable, PHP will flag an error.
A helpful addition to if statements is the elseif statement, which allows you to chain conditions together in a more intelligent way:
- if ($The_age < 10) {
- print "You're under 10";
- } elseif ($The_age < 20) {
- print "You're under 20";
- } elseif ($The_age < 30) {
- print "You're under 30";
- } elseif ($The_age < 40) {
- print "You're under 40";
- } else {
- print "You're over 40";
- }
You could achieve the same effect with if statements, but using elseif is easier to read. The downside of this system is that the $Age variable needs to be checked repeatedly.If you only have one statement of code to execute, you can do without the braces entirely. It's a readability issue.
So, these two code chunks are the same:
- if ($banned) {
- print "You are banned!";
- }
- if ($banned) print "You are banned!";
discuss this topic to forum
