Welcome to the World of PHP Page 8
Operations and Co mparisons
This statement actually consists of three expressions: the assignment operation, between $val3 and everything to the right of the equal sign; the addition operation between $val2 and the expression to the left of the plus sign; and finally, the subtraction operation within parentheses. Why the parentheses? It raises the issue of operator precedence. When an expression contains several operators, PHP has to determine in what order to perform the operat ions. Sometimes this matters greatly. Consider:
$val1 = 2 * 4 - 3;
What value should be assigned to $val1? If w e first multiply 2 and 4, and then subtract 3, the result would be 5. But if we first subtract 3 from 4, and then multiply that by 2 , the result would be 2. PHP has a set of precedence rules which define which operations take precedence -- in this case, multiplica tion is of higher precedence than subtraction, so in this case PHP would come up with the first interpretation, resulting in a value of 5.
To be honest, the operator precedence rules can be quite difficult to remember. Rather than memorizing them all, often a better approach is to use parentheses to group operations -- this way, we can determine our own precedence. For example, we can u se parentheses to determine either of the previous two interpretations:
$val1 = (2 * 4) - 3;
$val1 = 2 * (4 - 3) ;
The first example will yield 5, while the second will yield 2. It''s usually better to use parenthetical grouping this wa y rather than relying on PHP''s implicit precedence rules because 1) they can be difficult to remember, and 2) the above code will b e easier to understand by a human at a later time.
String operators work with characters rather t han numbers. And of course you can''t really add, subtract, or multiply the words "hot" and "tamale." Then again , maybe you can "add" them, if by that you mean string them together. In fact, this is called concatenation, and it is the only string operator available in PHP. Rather than a plus sign, the PHP concatenation operator is a single dot, or period. I magine that $firstName contains "Woodrow".
$fullName = $firstName . "Wilson";
Now, $ fullName will contains "WoodrowWilson". Whoops -- we''d probably want a space between those words, something to keep i n mind when you concatenate strings:
$fullName = $firstName . " Wilson";
PHP''s basic assignment operator is already familiar, being a simple equal sign (=). You can also combine the e qual sign with an arithmetic or string operator to make a "shortcut" assignment. Confused? Let''s illustrate -- suppose th at you''d like to add a value of 5 to whatever value is currently contained in $val1:
$val1 += 5;
Thus, if $val1 was previously valued at 10, after the above statement it would contain 15. Similarly, you can use the same shortcut fo r string concatentation, if you want to add a string to the end of an existing string. Suppose $name contains "Thelonius ":
$name .= " Monk";
And now $name will be "Thelonius Monk".
Comparing two values is frequently useful -- is a purchase total below a certain threshold, or is does one word come bef ore another alphabetically? PHP''s team of comparison operators is here to help! A comparison operator ultimately retu rns either a true or a false value, depending on the result of the comparison. What you do with this result value can vary, and we''ll see this more closely when we talk about control statements. For the examples below, let''s assume the following va riable values:
PHP Comparison Operators
|Equality||Tests whether two expression s are of equal value. For strings, the two must be exactly the same.|| |
$val1 == $val2
$name1 == $name2
|Inequality||Tests whether two expressions are not equal, or for strings, not exactly the same.|| |
$val1 != $val2
$name1 != $name2
|Less than||Tests whether the left-hand expression ha s a value smaller than the right-hand expression. For strings, smaller means "comes before alphabetically."|| |
$val1 < $val2
$name1 < $name2
|Greater than||Tests whether the left-hand expressi on has a value greater than the right-hand expression. For strings, greater means "comes after alphabetically."|| |
$val1 > $val2
$name1 > $name2
|Less than or equal to||Tests whether the left-hand expression is of equal or lesser value than the right-hand expression.|| |
$val1 <= 15
$name1 <= "Martin"
|Greater than or equal to||Tests whether the left-hand expression is of equal or g reater value than the right-hand expression.|| |
$name2 >= "Manfred"
Let''s raise the stakes: comparing t wo values is one thing, but sometimes you need something more, such as the ability to compare several comparisons! Let''s be logi cal about this, and use PHP''s logical operators. Suppose for example that you need to know whether $val1 i s of lesser value than $val2, and also whether $name1 is of greater value than $name2.
Remember t hat each comparison expression yields a true or false value. For the record, PHP does not actually possess true or false values, kno wn as Boolean data types. Rather, PHP considers the numeric value zero (0), or the string values "0" or empty "" to be false. Any other value, such as 1, or 3, or "cat", is considered to be true. A logical operator compares these true or false values. For example, using PHP''s and logical operator:
($val1 < $val2) and ($name1 > $name2)
Because this code fragment is not a full statement, there is no closing semicolon. Rather, we see parentheses grouping two expres sions. The left-hand expression will evaluate to the value false, if we assume the example values used in the comparison operator table. The right-hand expression will also be false, since "Martin" does not come after &quo t;Michael" alphabetically. The and operator will therefore compare the values "false and false". Because this operator returns true only when both expressions are true, the above expression will ultimately return false. That can be hard to fo llow, so let''s look at a summary table.
PHP Logical Operators
|And||True if both left-hand and right-hand expressions are true.|| |
(5>2) and (3>2) (2>5) and (3 >2)
|Or||True if either left-hand or right-hand expression is true.|| |
(5>2) or (2>7) ("h">"a") or (1<3)
|Exclusive Or||True if either left-hand or right-hand expression is true < i>unless both are true.|| |
("ar">"ac") xor (9>2) (3==3) xor ("f"==&q uot;f")
|N ot||Negates the expression it precedes, true if the expression is not true.|| |
Understanding all of these ope rators can certainly be exhausting!
Just as eye fatigue sets in -- a jolt! We''re about to string together everything we''ve learned so far, with the magic of control statements. Things will get much more exciting from here on in.