Logical Operators
These are the ones that allow you to create more elaborate constructs (uh….better stuff than we’ve done so far?)
In PHP, one example of a TRUE condition is simply a variable name that has a value,
such as: $variable = 5;
if ($variable) {…. and so on.
A condition is also TRUE if it make logical sense,
such as: if (5 >= 3) {… and so on
The condition will be FALSE if it refers to a variable that has no value, or if I”ve created an illogical construct,
such as: if (5 <= 3 ) {…. this is obviously wrong, and always will be
In PHP, the exclamation mark (!) is the NOT operator. You can use it to invert the TRUE/FALSE status of a statement,
such as: $var = “value”;
if ( $var ) {… this will be TRUE
if ( ! $var ) {… this will be FALSE
and written another way:
isset ($var); this will be TRUE
! isset ($var); this will be FALSE
! empty ($var); this will be TRUE
To go beyond simple one-part conditions, PHP supports five more types of logical operators:
two versions of “and” (and and &&)
two versions of “or” (OR and || ) The character “|” is called the pipe,
and you use two together for this.
one version of “or not” (XOR)
When there are two options for any one operator, they differ only in precedence - and can be used interchangeably.
Using parentheses and logical operators you can create even more complex if conditions - for an AND condtion, every part must be TRUE in order for the whole condition to be true, and with OR - only one of the parts of the condition needs to be TRUE.
Nesting Conditions
Conditions can also be nested inside one another (and this can get very confusing, I can tell already).
The key to doing this is to make the interior condition as the “statements” part of the exterior condition,
as so: if ( condition1 ) {
2 statements;
} else { this is the statement 2 else
2 other statements;
} this is the end of 2
} else { condition 1 else
other statements;
} end of condition 1
Whew! Have to really watch yourself on this, I think!
Two important things to remember:
In order for the statements that are the result of a conditional to be
executed, the conditional must have a TRUE value
By using parentheses, you can ignore rules of precedence, and
ensure that your operators are addressed in the order that
you want.
Bunch of stuff to absorb there - need to re-read and get this firmly in my mind.
and that’s my 15 for today! Tomorrow we’ll put this new knowledge to use!
Recent Comments
No comments yet.
Please login to comment.
