Tài liệu PHP and MySQL by Example- P3 ppt

50 387 0
Tài liệu PHP and MySQL by Example- P3 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

constant!to!the!following!script!that!will!define!a! COPY_RIGHT !constant!containing! your! SITE_NAME !with!the!copyright!symbol!appended!(concatenated)!to!it.!Display! the!constants!and!their!corresponding!values!in!an!HTML!table.!Hint:!See! http://www.desilva.biz/php/constants.html.!! <?php> // Define your site name, since it does NOT change // anywhere within your script. define( 'SITE_NAME', 'Your site' ); // Define the current year, possibly to use in your copyright // statement or for 'date' calculations. define( 'THIS_YEAR', date('Y') ); ?> ! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Chapter 5. Operators “Operator, give me the number for 911.” —Dan Castellaneta 5.1. About PHP Operators and Expressions Data objects can be manipulated in a number of ways by the large number of operators provided by PHP. Operators are symbols, such as +, -, =, >, and <, that produce a result based on some rules. An operator manipulates data objects called operands; for example, 5 and 4 are operands in the expression 5 + 4. Operators and operands are found in expressions. An expression combines a group of values to make a new value, n = 5 + 4. When you terminate an expression with a semicolon, you have a complete statement; for example, n = 5 + 4; !"#$%&&'()* sum * = * 5 * + * 4 * )%+*,-./%* (#%$-0($* (#%$-)1* (#%$-0($* (#%$-)1* * In the numeric expression 5 + 4 - 2, three numbers are combined. The operators are the + and - signs. The operands for the + sign are 5 and 4. After that part of the expression is evaluated to 9, the expression becomes 9 - 2. After evaluating the complete expression, the result is 7. Because the plus and minus operators each manipulate two operands, they are called a binary operators. If there is only one operand, the operator is called a unary operator, and if there are three operands, it is called a ternary operator. We’ll see examples of these operators later in the chapter. The operands can be either strings, numbers, booleans, or a combination of these. Some of the operators we have already used are the concatenation operator to join two strings together, the reference operator to create an alias for a variable, and the assignment operator to assign a value to a variable. Now let’s look at a whole plethora of additional PHP operators and see how they manipulate their operands. 5.1.1. Assignment An assignment statement evaluates the expression on the right side of the equal sign and assigns the result to the variable on the left side of the equal sign. The equal sign is the assignment operator. $total = 5 + 4; $name = "Tony"; * 5.1.2. Precedence and Associativity When an expression contains a number of operators and operands, such as 5 * 4 + 3 / -2.2, and the order of evaluation is ambiguous, then PHP must determine what to do. This is where the precedence and associative rules come Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. in. They tell PHP how to evaluate such an expression. Precedence refers to the way in which the operator binds to its operand; that is, should addition be done before division or should assignment come before multiplication. The precedence of one operator over another determines what operation is done first. As shown in the precedence table (see Table 5.1), the operators are organized as a hierarchy, with the operators of highest precedence at the top, similar to a social system where those with the most power (or money) are at the top. In the rules of precedence, the multiplication operator is of higher precedence than the addition operator, technically meaning the operator of higher precedence binds more tightly to its operands. The assignment operators are low in precedence and thus bind loosely to their operand. In the expression sum = 5 + 4, the equal sign is of low precedence so the expression 5 + 4 is evaluated first and then the result is assigned to sum. Parentheses are of the highest precedence. An expression placed within parentheses is evaluated first; for example, in the expression 2 * ( 10 - 4 ), the expression within the parentheses is evaluated first and that result is multiplied by 2. When parentheses are nested, the expression contained within the innermost set of parentheses is evaluated first. Table 5.1. Precedence and Associativity (Highest to Lowest) Operator( Description( Associativity( () * 2-$%)03%&%&* 4%50*0(*$'630 7-8 * new * 9$%-0%&*-)*(:;%<0* =()-&&(<'-0',%* [ * >$$-?*&/:&<$'#0* @'630*0(*.%50* ++ -- * >/0(*')<$%A%)0B*1%<$%A%)0* =()-&&(<'-0',%* ! ~ - * 4(6'<-.*)(0B*:'0+'&%*)(0B*)%6-0'()* =()-&&(<'-0',%* (int) (float) * (string) (array) (object) * 9-&0* ** @ * C)3':'0*%$$($&* ** * / % * D/.0'#.?B*1','1%B*A(1/./&* 4%50*0(*$'630* + - . * >11B*&/:0$-<0B*&0$')6*<()<-0%)-0'()* 4%50*0(*$'630* << >> * E'0+'&%*.%50*&3'50B*$'630*&3'50* 4%50*0(*$'630* < <= * 4%&&*03-)B*.%&&*03-)*($*%F/-.*0(* 4%50*0(*$'630* > >= * G$%-0%$*03-)B*6$%-0%$*03-)*($*%F/-.* 0(* ** = = != * !F/-.*0(B*)(0*%F/-.*0(* =()-&&(<'-0',%* = = = != = * C1%)0'<-.*0(*H&-A%*0?#%IB*)(0* '1%)0'<-.*0(* ** & * E'0+'&%*>=J* 4%50*0(*$'630* ^ * E'0+'&%*KL@* ** | * E'0+'&%*L@* ** Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Table 5.1. Precedence and Associativity (Highest to Lowest) Operator( Description( Associativity( && * 4(6'<-.*-)1* 4%50*0(*$'630* || * 4(6'<-.*($* 4%50*0(*$'630* ? : * M%$)-$?B*<()1'0'()-.* 4%50*0(*$'630* = += -= *= /= %= <<= >>= * >&&'6)A%)0* @'630*0(*.%50* and * 4(6'<-.*>=J* 4%50*0(*$'630* xor * 4(6'<-.*KL@* 4%50*0(*$'630* or * 4(6'<-.*L@* 4%50*0(*$'630* B*H<(AA-I* 4'&0*&%#-$-0($B*%0<N* 4%50*0(*$'630* * [a] Not listed in the PHP manual, but seems to behave the same as in other languages. Associativity refers to the order in which an operator evaluates its operands: left to right, in no specified order, or right to left. When all of the operators in an expression are of equal precedence (see Table 5.1), normally the association is left to right; for example, in the expression 5 + 4 + 3, the evaluation is from left to right. In the following statement, how is the expression evaluated? Is addition, multiplication, or division done first? In what order, right to left or left to right? Example 5.1. <html> <head><title>Precedence and Associativity()</title> </head> <body bgcolor="lightgreen"> <font face = "arial" size = '+1'> <?php 1 $result = 5 + 4 * 12 / 4; print "$result = 5 + 4 * 12 / 4"; 2 $result = (5 + 4) * 12 / (4 - 2); print "<br />$result = ( 5 + 4 ) * 12 / (4 - 2) "; ?> </body> Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Explanation O* M3%*($1%$*(5*-&&(<'-0','0?*'&*5$(A*.%50*0(*$'630N*D/.0'#.'<-0'()*-)1*1','&'()*-$%*(5*-*3'63%$* #$%<%1%)<%*03-)*-11'0'()*-)1*&/:0$-<0'()B*-)1*-11'0'()*-)1*&/:0$-<0'()*-$%*(5*-*3'63%$* #$%<%1%)<%*03-)*-&&'6)A%)0N*M(*' /&0$-0%*03'&B*+%P */&%*#-$%)03%&%&*0(*6$(/#*03%* (#%$-)1&*-&*03%?*-$%*6$(/#%1*:?*2Q2N*C)*5-<0B*'5*?(/*+-)0*0(*5($<%*#$%<%1%)<%B*/&%*03%* #-$%)03%&%&*-$(/)1*03%*%"#$%&&'()*0(*6$(/#*03%*(#%$-)1&*')*03%*+-?*?(/*+-)0*03%A* %,-./-0%1N*M3%*5( (+')6*0+(*%"-A#.%&*#$(1/<%*03%*&-A%*$%&/.0N** var result = 5 + 4 * 12 / 4; * <(/.1*:%*+$'00%)** result = (5 + ( ( 4 * 12 ) / 4)); * R* C)*03'&*%"-A#.%B*03%*%"#$%&&'()&*%)<.(&%1*+'03')*#-$%)03%&%&*-$%*%,-./-0%1*5'$&0N*M3%* * * -)1* / *-$%*%,-./-0%1*.%50*0(*$'630*:%<-/&%*03%?*-$%*(5*03%*&-A%*#$%<%1%)<%N*L/0#/0*(5*03'&* %"-A#.%*'&*&3(+)*')*S'6/$%*TNON* * Figure 5.1. Output from Example 5.1. * Table 5.1 summarizes the rules of precedence and associativity for the PHP operators. The operators on the same line are of equal precedence. The rows are in order of highest to lowest precedence. Example 5.2. <html> <head><title>Precedence and Associativity()</title> </head> <body bgcolor="lightgreen"> <font face = "arial" size = '+1'> <?php 1 $result = 5 + 4 * 12 / 4; 2 print "$result = 5 + 4 * 12 / 4"; 3 $result = (5 + 4) * (12 / 4) ; 4 print "<br />$result = ( 5 + 4 ) * (12 / 4) "; ?> </body> </html> Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Explanation O* M3%*,-$'-:.%B*<- %1* $result B*'&*-&&'6)%1*03%*$%&/.0*(5*03%*%"#$%&&'()N** $result = 5 + 4 * 12 / 4; produces: $result = 5 + 48 / 4 produces: $result = 5 + 12 and finally the sum: 17 * E%<-/&%*A/.0'#.'<-0'()*-)1*1','&'()*-$%*3'63%$*()*03%*#$%<%1%)<%*0-:.%*03-)*-11'0'()B* 03(&%*%"#$%&&'()&*+' *:%*%,-./-0%1*5'$&0B*-&&(<'-0')6*5$(A*.%50*0(*$'630N* R* M3%*$%&/.0*(5*03%*#$%,'(/&*%,-./-0'()B*03%*,-./%*(5* $result B*'&*&%)0*0(*03%*:$(+&%$N* U* M3%*%"#$%&&'()&*%)<.(&%1*')*#-$%)03%&%&*-$%*%,-./-0%1*5'$&0*-)1*03%)*A/.0'#.'%1N* $result = ( 5 + 4 ) * ( 12 / 4 ); produces: 9 * 3 $result = 9 * 3 results in: 27 * V* M3%*$%&/.0*(5*03%*#$%,'(/&*%,-./-0'()B*03%*,-./%*(5* $result B*'&*&%)0*0(*03%*:$(+&%$N*W%%* S'6/$%*TNRN* * Figure 5.2. Output from Example 5.2. * 5.1.3. Arithmetic Operators Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). See Table 5.2. Table 5.2. Arithmetic Operators Operator/Operands( Function( x + y * >11'0'()* x – y * W/:0$-<0'()* x * y * D/.0'#.'<-0'()* Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Table 5.2. Arithmetic Operators Operator/Operands( Function( x / y * J','&'()* x % y * D(1/./&* * Example 5.3. <html> <head><title>Arithmetic Operators</title></head> <body bgcolor="#ccccff" text="000033"> <h2>Arithmetic operators</h2> <p> 1 <?php> 2 $num1 = 5; $num2 = 7; 3 $result = $num1 + $num2; print "<h3>$result = $num1 + $num2 <br />"; 4 $result = $result + (10 / 2 + 5) % 7; print "$result = 12 + (10 / 2 + 5) %7<br /></h3>" ; ?> </body></html> Explanation O* M3'&*'&*03%*&0-$0*(5*-*2Q2*#$(6$-AN* R* X-$'-:.%&* $num1 *-)1* $num2 *-$%*1%<.-$%1*-)1*-&&'6)%1*,-./%&*T*-)1*YB*$%&#%<0',%.?N* U* M3%*&/A*(5* $num1 *-)1* $num2 *'&*-&&'6)%1*0(* $result *-)1*#$')0%1*()*.')%*UN* V* M3'&*-$'03A%0'<*(#%$-0'()*' /&0$-0%&*#$%<%1%)<%*-)1*-&&(<'-0','0?N*M3%*%"#$%&&'()*')* #-$%)03%&%&*'&*%,-./-0%1*5'$&0B*03%)*03%*A(1/.%*(#%$-0($*H03%* % *&'6)I*+' *1','1%*03-0* $%&/.0*:?*Y*-)1*$%0/$)*03%*$%A-')1%$B*-)1*-11'0'()*'&*#%$5($A%1*.-&0N*M(*&3(+*03%*($1%$* (5*%,-./-0'()B*+%*<-)*#/0*#-$%)03%&%&*-$(/)1*- *(5*03%*%"#$%&&'()&N*W0-$0*%,-./-0')6* +'03*03%*'))%$A(&0*&%0*(5*#-$%)03%&%&*5'$&0* (10/2 + 5) B*03%)*03%*)%"0*&%0B*-)1*&(*()Z* (12 +( (10 / 2 + 5) %7)) * W%%*S'6/$%*TNU*5($*(/0#/0*(5*03'&*%"-A#.%N * Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Figure 5.3. Output from Example 5.3. 5.1.4. Short Circuit Assignment Operators The short circuit assignment operators allow you to perform an arithmetic or string operation by combining an assignment operator with an arithmetic or string operator. For example, $x = $x + 1 can be written $x+=1. Table 5.3. Assignment Operators Operator Example Meaning = $x = 5; >&&'6)* 5 *0(*,-$'-:.%* $x N += $x += 3; >11* 3 *0(* $x *-)1*-&&'6)*$%&/.0*0(* $x N -= $x -= 2; W/:0$-<0* 2 *5$(A* $x *-)1*-&&'6)*$%&/.0*0(* $x N *= $x *= 4; D/.0'#.?* $x *:?* 4 *-)1*-&&'6)*$%&/.0*0(* $x N /= $x /= 2; J','1%* $x *:?* 2 *-)1*-&&'6)*$%&/.0*0(* $x N %= $x %= 2; J','1%* $x *:?* 2 *-)1*-&&'6)*$%A-')1%$*0(* $x N * Example 5.4. <html> <head><title>Arithmetic Operators</title></head> <body bgcolor="#99ff66"> <h2>Shortcut Operators</h2> <font size="+1" > 1 <?php> //Using shortcuts 2 $num=10; Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. print "10 is assigned to \$num.<br />"; 3 $num += 2; print "\$num += 2; \$num is $num. <br />"; 4 $num -= 1; print "\$num -= 1; \$num is $num. <br />"; 5 $num *= 3; print "\$num *= 3; \$num is $num. <br />"; 6 $num %= 5; print "\$num %= 5; \$num is $num.<br />"; ?> </body> </html> Explanation O M3%*2Q2*#$(6$-A*&0-$0&*3%$%N R O[*'&*-&&'6)%1*0(*03%*,-$'-:.%* $num N U M3%*&3($0</0*-&&'6)A%)0*(#%$-0($B* += B*-11&* 2 *0(*03%*,-$'-:.%B* $num N*M3'&*'&* %F/',-.%)0*0(Z* $num = $num + 1; V M3%*&3($0</0*-&&'6)A%)0*(#%$-0($B* -= B*&/:0$-<0&* 1 *5$(A*03%*,-$'-:.%B* $num N*M3'&* '&*%F/',-.%)0*0(Z* $num = $num - 1; T M3%*&3($0</0*-&&'6)A%)0*(#%$-0($B* * B*A/.0'#.'%&*03%*,-$'-:.%* $num *:?* 3 N*M3'&*'&* %F/',-.%)0*0(Z* $num = $num * 3; \ M3%*&3($0</0*-&&'6)A%)0*A(1/./&*(#%$-0($B* % B*?'%.1&*03%*')0%6%$*-A(/)0*03-0* $%A-')&*-50%$*03%*&<-.-$* $num *'&*1','1%1*:?* 5 N*M3%*(#%$-0($*'&*<- %1*03%* A(1/./&*(#%$-0($*($*$%A-')1%$*(#%$-0($N*M3%*%"#$%&&'()* $num %=5 *'&* %F/',-.%)0*0(Z* $num = $num % 5; N*W%%*S'6/$%*TNV*5($*(/0#/0*(5*03'&*%"-A#.%N * Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Figure 5.4. Output from Example 5.4. 5.1.5. Autoincrement and Autodecrement Operators To make programs easier to read, to simplify typing, and, at the machine level, to produce more efficient code, the autoincrement (++) and autodecrement (--) operators are provided. The autoincrement operator performs the simple task of incrementing the value of its operand by 1, and the autodecrement operator decrements the value of its operand by 1. The operator has two forms: The first form prefixes the variable with either ++ or -- (e.g., ++$x or --$x); the second form postfixes (places the operator after) the variable name with either ++ or -- (e.g., $x++, x--). For simple operations, such as $x++ or $x--, ++$x or --$x, the effect is the same; both ++$x and $x++ add 1 to the value of $x, and both --$x and $x-- subtract one from the value of $x. See Table 5.4 for examples. Table 5.4. Autoincrement and Autodecrement Operators Operator Function What(It(Does Example ( ++$x 2$%')<$%A%)0 >11&*O*0(* $x $x = 3; $x++; $x *'&*)(+*V $x++ 2(&0')<$%A%)0 >11&*O*0(* $x $x = 3; ++$x; $x *'&*)(+*V ––$x 2$%1%<$%A%)0 W/:0$-<0&*O*5$(A* $x $x = 3; $x;–– $x *'&*)(+*R $x–– 2(&01%<$%A%)0 W/:0$-<0&*O*5$(A* $x $x = 3; --$x; $x *'&*)(+*R * Now you have four ways to add 1 to the value of a variable: $x = $x + 1; $x += 1; $x++; ++$x ; * You also have four ways to subtract 1 from the value of a variable: $x = $x - 1; $x -= 1; $x--; --$x; * In Chapter 6, “Strings,” these operators are commonly used to increment or decrement loop counters. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... Figure 5.14 The PHP script output from Example 5.11     The Difference Between &&/|| and the Words and/ or You can use the words and and or to replace && and ||, respectively, but they are not exactly the same because they have a different precedence Look at the precedence table (Table 5.1 on page 110) and notice that the and and or operators are lower on the table than the corresponding && and || In fact,... < ?php 1 $x = 5; $y = 6; $z = 0; 2 $result = $x && $y && $z; echo "$x && $y && $z yields " (int)$result ".\n"; 3 $result = $x and $y and $z; echo "$x and $y and $z yields " (int)$result ".\n"; 4 $result = ($x and $y and $z); echo "($x and $y and $z) yields " (int)$result ".\n"; ?> Explanation 1 Three  variables  are  intialized and. .. pow(3,2); // 1000 rand(start,finish) Generate  a  random   integer  between   start and  finish echo rand(1,10); // 5 echo rand(1,10); // 7 echo rand(1,10); // 10 round() Rounds  a  float echo round(6.4); // 7 sqrt() Square  root echo sqrt(81); srand() Seed  the  random   number  generator   // 9 echo pow(10,3); // 6 echo round(6.5); // 9   5.1.7 Casting Operators As defined earlier, PHP is a loosely... logical operators are the logical AND, logical OR, and logical NOT The symbol for AND is &&, the symbol for OR is || The English version for && is and and for || is or The only difference is that of precedence, the English version being of lower precedence The && (and) Operator, the Logical AND We all know the meaning of the English statement, “If you have the money and I have the time ” Whatever...  input,   encode  it, and  send  it  to  the  server  where PHP  resides 5,   The  && and  the  alternative  word and  require  that  both  of  its  operands  are  true  for  the   6 expression  to  be  true  In  this  example,  all  of  the  expressions  must  be  true  for  line  6  to   be  executed;  that  is,  if  the  value  of  $age  is  greater  than  12 and  less  than  20 and  not  an   empty... http://dictionary.reference.com/search?r=10&q=concatenation The process of joining strings together is called concatenation The PHP string concatenation operator is a dot (.) Its operands are two strings It returns the concatenation of its right and left operands If either operand is a number and the other is a string, PHP still concatenates them as strings "pop" "corn" // results in "popcorn" "Route " 66 // results in... $x on one line and on the next line assign a string to $x; you can compare numbers and strings, strings and booleans, and so on PHP automatically converts values when it assigns values to a variable or evaluates an expression If data types are mixed, that is, a number is compared to a string, a boolean is compared to a number, a string is compared to a boolean, PHP must decide how to handle the expression...  the   logical AND 4 The  ||  operator  expects  only  one  of  its  operands  to  be  true  if  the  whole  expression  is  to   be  true  50 || 100  is  true  because  the  first  operand  evaluates  to  a  nonzero  value   Because  50  is  true and  only  one  operand  must  be  true,  the  evaluation  stops  here and  1  is   returned 5 The  xor  operator  expects  one  operand  to  be  true,... numbers, numeric values are compared; for example, is 50 > 45? A boolean value of either true or false is returned PHP compares its operands numerically if: 1 Both operands are numbers: 4 > 5 2 One operand is a number and the other is a string consisting of all numbers: "54" > 6 3 Both operands are strings containing all numbers: "56" < "57" For example: $x > $y $x  is  greater  than  $y $x >= $y $x  is... than 10, then do something.” In the case of the logical AND (&&), if the first expression returns true and the second expression also returns true, then the whole expression is true A numeric operand is true if it evaluates to any number that is not zero 5, -2, and 74 are all true 0 is false For example, when using the && (AND) operator, both operands must be true for the whole expression to be true . Castellaneta 5.1. About PHP Operators and Expressions Data objects can be manipulated in a number of ways by the large number of operators provided by PHP. Operators. to $x on one line and on the next line assign a string to $x; you can compare numbers and strings, strings and booleans, and so on. PHP automatically converts

Ngày đăng: 24/12/2013, 03:17

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan