A conditional statement is a way to make it so a block of code will only be executed when certain conditions are met. The conditions can be a comparison of one or more variables or constants or the result of a variable test on one or more variable or a combination of any of the preceding kinds of tests. A block of code in a conditional statement either happens ot it does not. A looping statement is like a conditional statement but the block of code in it is executed over and over until the conditional test tells it to stop.
An if statement consists of a test and a block of code to execute if the test is met and the syntax looks like this:
<?
if ( test expression)
{
# block of code
}
?>
One of the most basic tests is if a variable even exists or not. How could we not know if a variable exists and why would it make a difference? in Forms Pt. 2 we looked at a form that used isset() to test if input had been submitted then divided the input by a constant. Consider a form where the denominator is inputed by a user instead:
Looks simple enough. Just divide $x by $y and show the answer. So why do you get error messages before you even enter anything? If you look at the code closely, you'll see that $x and $y are never defined so as far as the PHP is concerned, they don't exist until called by the line:
echo "$x divided by $y is $z";
At that point, they are created but since you didn't give them a value, they are assigned a value of zero. When it tries to divide by zero, you get the error messages. If you enter some numbers into the form, $x and $y are created when they are submited so PHP can finish the math without errors. Below is the same script with an if statement that uses isset() to test for the existance of $x. If $x is not set, the math is not executed and there are no error messages. If $x is inputed, $x is set and the math is executed.
You can also compare inputed data to a string as in this password script:
This script does two tests on $pass. First it uses the non-equivalance operater to test if $pass is not the same as the password and if not, echos a prompt for the password. The next test uses the equivalance operater to test if $pass is the same as the password. If it is, a success message is echoed.
Important! Don't forget the difference between the equivalance operater and the assignment operater. If you use the assignment operater instead of the equivalance operater to compare two values, it will always test true. If you just assigned the value password to $pass then then $pass is password, right?
The example below compares the sum of $x and $y to 10 and executes different blocks of code depending on whether the sum is greater than, less than or equal to 10.
The example above shows how conditional statements can be nested. There are two main tests; !isset($x) and isset($x). The tests $x+$y > 10, $x+$y < 10 and $x+$y == 10 are inside of isset($x) so they are not executed if $x is not set.
Nested conditional statements are a big source of bugs for me. What typically happens is that I manage to erase the closing } for the outer statement and when I try to view the page I get an error message something like,
Unexpected $ on line 80.
When I check my code, I run into two problems; first, all $'s are accounted for and second, my code is only 50 lines long! When PHP gets to the missing }, it doesn't recognize that there is supposed to be a line end there and keeps right on going to the next one or, failing to find another one, right off the page and tells you that the error is there.
A complex conditional statement is one that makes two or more tests in one step using logical operaters. For example, if a user is supposed to enter a number between 0 and 100:
<?
if ( ($x<0) && ($x>100) )
{ echo "x must be between 0 and 100"; }
?>
|
|
|