Youth Tech's Online PHP Class: Lesson Four
If Statements [Chat: 8/9/02]
Hosted by YTCC Spark
Welcome back to PHP Class! If you have any questions, as always, don't hesitate
to email me. Or, the
preferred method, bring your questions to Chat, 10PM EST, every Friday!
If Statements
In any programming language, you have to be able to make decisions
based on the values of variables. In order to make these
decisions, we must use if statements. Within the if statement, we
have a boolean expression to be evaluated. ALL boolean expressions
evaluate to either true or false. You may use any of several
comparison operators in these expression, the most common being:
== is equal to
!= is not equal to
< is less than
> is greater than
<= is less than or equal to
>= is greater than or equal to
The basic syntax of each if statement is as follows:
if(BOOLEAN_EXPRESSION){
STATEMENTS TO BE EXECUTED IF EXPRESSION IS TRUE
}
For example:
if($A==$B){
print "A equals B";
}
If-Else Statements
An If-Else statement allows you to specify what should be done if the
boolean expression does not evaluate to true. For example, the
following should be fairly obvious as to the intentions:
if($A==$B){
print "A equals B";
} else {
print "A does not equal B";
}
If-Else If-Else Statements
This is simply a method for providing more than two possible
choices.
if($A==$B){
print "A equals B";
} else if ($A>$B) {
print "A is greater than B";
} else {
print "A is less than B";
}
See sample source for lesson
four.
See output of sample source.
Back to the Index