We’ve already described simple examples of assignment, in which a variable is assigned the value of an integer, string, or value of some other data type. The value on the right side of the equal sign is actually the simplest example of an expression. An expression is anything that can be reduced to a single value, for example the sum1 + 2is an expression with an integer value of 3. Expressions can be complex combinations of operators and values, just as in mathematics. Examples of expressions (the first involving integers, the second involving integers and one floating point number) are: 6 + 3 -2 The basic syntax for expressions in PHP is taken from the C language and is familiar to someone who has worked in almost any high-level programming language. Here are some examples: // Assign a value to a variable There are many mathematical functions available in the math library of PHP for more complex tasks. We introduce some of these in the next chapter. String expressions can be created using the dot-operator (.) to concatenate two strings: // Assign a string value to a variable The following are all equivalent. The syntax you use is a matter of taste. echo "test string"; The first contains a single string. The second contains an expression combining two strings, while the third contains two arguments to theechocommand. The values returned from functions and many statements can be used as expressions including a variable assignment. In the following example, the assignment($x=42)is used as an integer expression with the value of 42: // assign both $y and $x the value 42 The parentheses are not needed in the example above; however, they highlight the fact that$x = 42is an expression. PHP automatically converts types when combining values in an expression. For example, the expression4 + 7.0contains an integer and a float; in this case, PHP considers the integer as a floating-point number, and the result is of type float. The type conversions are largely straightforward; however, there are some traps, which are discussed later in this chapter. Operator precedenceThe term precedence in mathematics and programming refers to the decision concerning which operator is evaluated first. For instance, in the following expression, by convention, the multiplication operator is evaluated first, leading to a value of 32: 2 + 5 * 6 PHP defines the precedence of operators in an expression similar to how it is done in other languages. Multiplication and division occur before subtraction and addition, and so on. However, reliance on evaluation order leads to unreadable, confusing code. Rather than memorize the rules, we recommend you construct unambiguous expressions with parentheses, because parentheses have the highest precedence in evaluation. For example, in the following fragment$variableis assigned a value of 32 because of the precedence of multiplication over addition: $variable = 2 + 5 * 6; But the result is much clearer if parentheses are used: $variable = 2 + (5 * 6); Conditions and BranchesConditionals add control to scripts and permit choices. Different statements are executed depending on whether expressions are true or false. There are two branching statements in PHP: if, with the optionalelseclause, andswitch, usually with two or morecaseclauses. if...else Statement The if statement conditionally controls execution. The basic format of an if statement is to test whether a condition is trueand, if so, to execute one or more statements. The followingifstatement executes theprintstatement and outputs the string when the conditional expression,$varis greater than 5, istrue: if ($var > 5)
Multiple statements can be executed as a block by encapsulating the statements within braces. If the expression evaluates astrue, the statements within the braces are executed. If the expression isn’ttrue, none of the statements are executed. Consider an example in which three statements are executed if the condition istrue: if ($var > 5) Without the braces, anifstatement executes only the single, immediately following statement when the conditional expression evaluates totrue. Theifstatement can have an optionalelseclause to execute a statement or block of statements if the expression evaluates asfalse. Consider an example: if ($var > 5) It’s also common for theelseclause to execute a block of statements in braces, as in this example: if ($var < 5) Consecutive conditional tests can lead to examples such as: if ($var < 5) The indentation in the preceding example highlights the nested nature of the multiple tests. If consecutive, cascading tests are needed, theelseif statement can be used. The choice of which method to use is a matter of personal preference. This example has the same functionality as the previous example: if ($var < 5)
blog comments powered by Disqus |
|
|
|
|
|
|
|