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

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
*

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)
*

*
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

(#%$-)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";

*
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">

(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;



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

$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*
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



Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
The Autoincrement/Autodecrement and Assignment
The placement of the operators does make a difference in more complex expressions especially when part of an
assignment; for example, $y = $x++ is not the same as $y = ++$x. See Figure 5.5.
Figure 5.5. Start with: $y = 0; $x = 5;. See Example 5.5.
*
*
*
Example 5.5.
<html>
<head><title>Autoincrement and Autodecrement</title></head>
<body bgcolor="6699ff">
<?php>
1 $x=5; $y=0;
2 $y = ++$x;
//
add 1 to $x first; then assign to $y
print "<h3>Preincrement:<br />";
3 print "\$y is $y.<br />";
print "\$x is $x.<br />";
print "<hr>";
4 $x=5; $y=0;
5 $y = $x++;
print "Postincrement:<br />";
6 print "\$y is $y. <br />";
print "\$x is $x. <br />";
?>
</body>
</html>

B*
$y
*'&*
6
N
U M3%*)%+*,-./%&*(5*
$y
*-)1*
$x
*-$%*1'&#.-?%1*')*03%*:$(+&%$*+')1(+N
V M3%*,-$'-:.%&B*
$x
*-)1*
$y
B*-$%*-&&'6)%1*,-./%&*(5*
5
*-)1*
0
B*$%&#%<0',%.?N
T M3'&*0'A%*03%*#(&0')<$%A%)0*(#%$-0($*'&*-##.'%1*0(*
$x
N*M3'&*A%-)&*03-0*
$x
*+'..*
:%*')<$%A%)0%1*-50%$*03%*-&&'6)A%)0*'&*A-1%N*
5
*'&*-&&'6)%1*0(*03%*,-$'-:.%*
$y
B*
-)1*03%)*

N
\ M3%*)%+*,-./%&*(5*
$y
*-)1*
$x
*-$%*1'&#.-?%1*')*03%*:$(+&%$*+')1(+N*W%%*S'6/$%*
TN\N
*
Figure 5.6. Output from Example 5.5. 5.1.6. Some Useful Math Functions
Table 5.5 lists some of the math functions provided by PHP. The complete list can be found at the PHP Web site.
Table 5.5. Math Functions
Function Meaning Example
abs()

>:&(./0%*,-./%
echo abs(-5); // 5 echo abs(5.3); //
5

base_convert()

9(),%$0*-*)/A:%$*
:%0+%%)*-$:'0$-$?*
:-&%&
echo base_convert("ff",16,10); // 255
echo base_convert("a",16,2); // 1010
echo base_convert(11,10,8); // 13


J%<'A-.*0(*(<0-.
echo decoct(8); // 10 echo decoct(20);
// 24

floor()

@(/)1*5$-<0'()&*
1(+)
echo floor(6.2); // 6 echo floor(6.8);
// 6

getrandmax()

W3(+*.-$6%&0*
#(&&':.%*$-)1(A*
,-./%
echo getrandmax(); // returns 32767

hexdec()

Q%"-1%<'A-.*0(*
1%<'A-.
echo hexdec("ff"); // returns 255 echo
hexdec("a"); // returns 10

is_finite()

S')1&*+3%03%$*-*
,-./%*'&*-*.%6-.*5')'0%*
)/A:%$B*$%0/$)&*

echo octdec(10); // returns 8

pi()

G%0*,-./%*(5*#'
echo pi(); // 3.1415926535898
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Table 5.5. Math Functions
Function Meaning Example

pow()

!"#()%)0'-.*
%"#$%&&'()
echo pow(3,2); // 9 echo pow(10,3);
// 1000

rand(start,finish)

G%)%$-0%*-*$-)1(A*
')0%6%$*:%0+%%)*
start
*-)1*
finish

echo rand(1,10); // 5 echo rand(1,10);
// 7 echo rand(1,10); // 10

round()


(float)

H$%-.I S.(-0')6*#(')0
(string)

* W0$')6
(bool)

H:((.%-)I E((.%-)
(array)

* >$$-?*H&%%*93-#0%$*^B*_>$$-?&`I
*
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Format
variable = (cast operator) value;
*
Example:
$salary = "52000"; // Variable is assigned a string value $salary = (float)
$salary; // Value is forced to float and reassigned

Example 5.6.
9(1%*X'%+Z*
<html><head><title>Type Casting</title></head>
<body bgcolor="aqua">
<font face="verdana" size="+1">
<?php

B*'&*-&&'6)%1*-*&0$')6*<()0-')')6*&(A%*.%-1')6*)/A:%$&N
R M3%*)%+*0?#%*'&*#.-<%1*+'03')*#-$%)03%&%&B*<-/&')6*03%*,-$'-:.%B*
$string
B*0(*:%*
0%A#($-$'.?*<-&0*5$(A*-*&0$')6*1-0-*0?#%*0(*-)*')0%6%$N*M3%*($'6')-.*
$string
*+'..*)(0*:%*
<3-)6%1N*C0*'&*&0'..*-*&0$')6*0?#%B*:/0*
$number
*+'..*:%*-)*')0%6%$N*2Q2*$%0-')&*().?*03%*
.%-1')6*)/A:%$&*')*
$string
B*03/&*$%A(,')6*
dogs
*1/$')6*03%*0?#%*<-&0N
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
U
($total_seconds / 60)
*'&*<-&0*0(*-)*')0%6%$*:%5($%*-&&'6)')6*03%*$%&/.0*0(*
$minutes
N*
W%%*S'6/$%*TNY*5($*(/0#/0*(5*03'&*%"-A#.%N
*
Figure 5.7. Type casting. Output from Example 5.6. 5.1.8. Concatention Operator
Concatenation is from Late Latin concatenatio, from concatenare,
“to chain together,” from Latin con-, “with, together” + catena,
“a chain, a series.”

Explanation
O
$string1
*'&*-&&'6)%1*03%*&0$')6B*
"My dog"
a*
$string2
*'&*-&&'6)%1*03%*&0$')6B*
"has fleas"
N*
M3%&%*0+(*&0$')6&*+'..*:%*.')b%1*0(6%03%$*+'03*03%*<()<-0%)-0'()*(#%$-0($N
R
$string3
*'&*<$%-0%1*:?*<()<-0%)-0')6*
$string1
*-)1*
$string2
*0(6%03%$N*M3%*<(AA%)0*
&3(+&*-)(03%$*+-?*0(*/&%*03%*<()<-0%)-0'()*(#%$-0($Z*
$string1.= $string2
N*c3%)*
<(A:')%1*+'03*03%*-&&'6)A%)0*(#%$-0($B*
.=
B*
$string1
*+'..*:%*-&&'6)%1*'0&*,-./%*
<()<-0%)-0%1*0(*03%*,-./%*(5*
$string2
B*&-A%*-&Z*
$string1 = $string1 . $string2


$x != $y

$x
*'&*)(0*%F/-.*0(*
$y

$x > $y

$x
*'&*6$%-0%$*03-)*
$y

$x >= $y

$x
*'&*6$%-0%$*03-)*($*%F/-.*0(*
$y

$x < $y

$x
*'&*.%&&*03-)*
$y

$x <= $y

$x
*'&*.%&&*03-)*($*%F/-.*0(*
$y


M$/%
"william" == "William"

S-.&%
5 == 5.0

M$/%
"54" == 54

M$/%
"5.4" == 5.4

M$/%
null == null

M$/%
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Table 5.8. Equality Test with Strings and Numbers
Test Are(They(Equal?
-0 == +0

M$/%
false == false

M$/%
true == 1

M$/%
null == ""

true === 1

S-.&%
null === ""

S-.&%
*
5.1.10. Comparing Numbers
When the comparison operators are used to compare 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
*'&*6$%-0%$*03-)*
$y

$x >= $y

$x
*'&*6$%-0%$*03-)*($*%F/-.*0(*
$y

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
$x > $y

$x

</html>
Explanation
O M3%*,-$'-:.%&*
$x
*-)1*
$y
*-$%*-&&'6)%1*,-./%&B*0(*:%*<(A#-$%1*.-0%$*')*03%*
#$(6$-AN
R C5*03%*,-./%*(5*
$x
*'&*6$%-0%$*03-)*03%*,-./%*(5*
$y
B*-*:((.%-)*,-./%*(5*%'03%$*
1
*($*
""
*'&*$%0/$)%1*-)1*-&&'6)%1*0(*03%*,-$'-:.%*
result
N
U M3%*:((.%-)*$%&/.0*(5*03%*<(A#-$'&()*'&*1'&#.-?%1*:?*03%*:$(+&%$N*C0*'&*
true
*($*
Oa*
$x
*'&*6$%-0%$*03-)*
y
N
V C5*
$x
*'&*.%&&*03-)*


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status