Operation Precedence and Associativity

Một phần của tài liệu Professional PHP Programming phần 2 docx (Trang 25 - 31)

Precedence refers to the order in which different operations will be executed. For example, consider the expression 9 - 4 * 2. We might think that the result of this will be 10, since 9 - 4 is 5, and 5 * 2 is 10. However, the * operator takes precedence over the - operator, so the multiplication is performed before the subtraction. Therefore 9 - 4 * 2 in fact evaluates to 1: 4 * 2 is calculated first, and only then is the result subtracted from 9. If we wanted the subtraction to occur before the multiplication, we

Comment: Check chapter ref later

could explicitly dictate the precedence with brackets: 5 * (4 - 2). Using parentheses is always a wise way to avoid confusion and ambiguity when it comes to precedence.

Associativity refers to the order in which operations of the same precedence will occur. For example, division has a left-to-right associativity (often referred to simply as Left), so 8 / 4 / 2 is equivalent to (8 / 4) / 2, or 1 (and not 8 / (4 / 2), which would equal 4). Assignment, on the other hand, has a right-to-left associativity (Right), so, if we write:

$h = 8;

$i = 4;

$j = 2;

$h /= $i /= $j;

echo($h);

This is equivalent to:

$h = 8;

$i = 4;

$j = 2;

$i = $i / $j;

$h = $h / $i;

echo($h);

So it will print 4 rather than 1. The following table lists the operators showing those with the highest precedence first.

Operator Operation Associativity Number of

Operands

() Precedence establishment N/A Unary

new Object instantiation N/A Unary

[] Array index access Right Binary

! Logical not Right Unary

~ Bitwise not Right Unary

++ -- Incrementation, Decrementation Right Unary

@ Functional error suppression Right Unary

* / % Multiplication, Division, Modulus Left Binary + - . Addition, Subtraction, Concatenation Left Binary

<< >> Bitwise left shift, right shift Left Binary

< <= > >= Less than, Less than or equal to,

Greater than, Greater than or equal to N/A Binary

== != Equal to, Not equal to N/A Binary

& Bitwise AND Left Binary

^ Bitwise XOR Left Binary

| Bitwise OR Left Binary

&& Logical AND Left Binary

|| Logical OR Left Binary

?: Conditional Right Ternary

= += -= *= /=

.= %= &= != ~=

<<= >>=

Assignment Right Binary

and Logical AND Left Binary

xor Logical XOR Left Binary

or Logical OR Left Binary

, Multiple value evaluation Left Binary

The , operator is used to evaluate multiple parameters to functions. Functions are discussed in Chapter 7.

Building an Online Job Application Form

Recall from Chapter 4 that the line $avail = isset ($avail); in jobapp_action.php causes

$avail to have a value of 1 (true) or 0 (false). Suppose that we would like to display to the user whether or not the "Available immediately" checkbox element was checked.

We could display the value of the variable:

<HTML>

<!-- jobapp_action.php -->

<BODY>

<?php

// Convert to boolean $avail = isset ($avail);

echo ("Available immediately: " . $avail);

?>

</BODY>

</HTML>

This would not be a very user-friendly solution, however. We can't expect ordinary users to interpret "0"

to mean "no" and "1" to mean "yes". It would be much friendlier to use the conditional (ternary) operator to print "no" or "yes", depending on the value of $avail. However, we need to be careful; the natural inclination is to write the page like this:

<HTML>

<!-- jobapp_action.php -->

Comment: Check chapter ref later

Comment: Check chapter ref later

<BODY>

<?php

// Convert to boolean $avail = isset ($avail);

echo ("Available immediately: " . $avail ? "yes" : "no");

?>

</BODY>

</HTML>

So, what have we done here? The first line inside the echo statement has not changed. We still want to print the string "Available immediately: ", and then use the concatenation operator to join this string to the results of the conditional operation which tests the value of $avail. If it evaluates to true, it adds "yes" to the string; if false, it concatenates "no" to the string. The good news is that "yes"

is much friendlier than "1". The bad news is that this code has a logic error: it will produce the same output every single time it executes, regardless of the value of $avail:

What happened to "Available immediately: "? What happened to "no"? In a word:

precedence. The concatenation operator has a higher precedence than the ternary operator. Therefore, PHP first concatenates the string "Available immediately: " with the value of $avail, resulting in either "Available immediately: 1" or "Available immediately: 0". It then uses this entire newly-constructed string as the conditional operand of the ternary operator. Since a non- empty string always evaluates to true, the ternary operator will always produce the string "yes"; and it is this value that will be displayed by the echo statement. To correct the error, we can use parentheses around the entire ternary operator, thereby ensuring that only $avail will be evaluated as the conditional operand:

<HTML>

<!-- jobapp_action.php -->

<BODY>

<?php

// Convert to boolean $avail = isset ($avail);

echo ("Available immediately: " . ($avail ? "yes" : "no"));

?>

</BODY>

</HTML>

Summary

Operators are symbols that determine values by performing operations on other values, or operands. We can divide operators into three types, according to the number of operands they take: unary operators (such as !) take one operator, binary operands (such as +) take two operands, and the ternary operator (?:) takes three operands.

When a number of operations occur within a single expression, their order of execution is governed by two factors, the precedence and associativity of the operators. Precedence refers to the order in which different operations will be executed. Operations such as multiplication and division have a higher precedence than addition or subtraction, and therefore occur first. To override precedence, use parentheses:

$a = 2 + 3 * 4; // $a is 14

$a = (2 + 3) * 4; // $a is 20

Associativity refers to the order in which operations of the same precedence will occur. Addition and

subtraction have left-to-right associativity, whereas assignment has right-to-left associativity.

6

Statements

Statements provide the bones of an application. Conditional statements and loops give programs the basic decision-making ability necessary to complete most computing tasks. In this chapter, we will cover the syntax of these structures and further develop the online job application.

Một phần của tài liệu Professional PHP Programming phần 2 docx (Trang 25 - 31)

Tải bản đầy đủ (PDF)

(86 trang)