Upload
yulia-emelyanova
View
246
Download
0
Embed Size (px)
Citation preview
********************************************************** ! http://natahaus.ru/ This file was downloaded from natahaus.ru portal ********************************************************** http://www.natahaus.ru/ , . ! , . , . . . . . . , , , e-mail - .
PHP 5
""-
-
2005
32.973.26-018.2.75 15
681.3.07
""
. ..
. . ..
"" : [email protected], http://www.dialektika.com 115419, , / 783; 03150, , / 152
,
15 5 "".: . . .: "", 2005. 320 .: . . . .
ISBN 5-8459-0851-5 (.) Web- 5.
Web, , . -
.
.
5 Web > , Web- Apache !.
, , 1- Web.
32.973.26-018.2/5
.
, ,
, JOHN WJLEY&Sons, Inc.
Copyright 2005 by Dialektika Computer Publishing. Original English language edition Copyright 2004 by Wiley Publishing, Inc. All rights reserved including the right of reproduction in whole or in part in any form. This translation published
by arrangement with Wiley Publishing, Inc. For Dummies and Dummies Man are trademarks under exclusive license to Wiley Publishing, Inc. Used by
permission.
ISBN 5-8459-0851-5 (.) ISBN 0-7645-4166-8 (.)
- "", 2005 Wiley Publishing, Inc., 2004
16
I. 19 1. 21 2. 29 3. 43
II. 55 4. 57 5. 72 6. 89
III. 7. 113 8. 134 9. - 148
IV. - 165 10. Web- 167 11. Web- 188 12. 207 13. 231 14. 250
V. 263 15. ,
265 16. Web- 270
VI. 273 . 275 . 293 310
,
I. 1.
Web-
Web
5
2. Web-
Web-
Web-
-
3.
HTML- Web
16 L6 16 17 17 18
19
:> : :>2 :>2 :> :4
:>4 : :5 :>
27 21
29 29 : : 34 :i6 :\9 :\9 40 40 41 43 43 44 45 46 48 49 :0 :>1 :>2
II. 55 4. 57
57 58
58 59
60 61 62 63 63
63 64 66 66
67 Web- 67 68 69 70
5. 72 72
72 73
74 74 76
78 79 79 80 81 81 82
85 86 timestamp 87
6. 89 89
89 91 92 92
93 94
echo 95 list 95
7
!6 96 foreach (>7
98 ( ) 98 ( ) '19 11)0 101 102
102
103 103
104 105 105 106
, 107 107 $_SERVER $_ENV 108 $argv $argc 109
III. 7. 1 [3
113 114
115 116 1L6 119
1: > 1 if 121 switch 124
125 for 126 while 129 dcwhile 1: 1)1 1.32
8. 134 155
135 136 137
() 138 138 140 141
8
145 146
147 9. - 148
- 148 149 149 149 150 - 5 151
- 151 151 152 152
152 152 153 $this 154 154 154 155
157 158 161 161 162
IV. - 165 10. Web- 167
Web- 167 , Web- 168 168 169 Web- 169
Web- 170 HTML- 170
Web- 171 177 179 186
11. Web- 188 Web- 188 Web- 188
189 189 190
191 URL- 191
9
cookie 193 HTML- 194 195 200
200 200 201 202 202
JavaScript 205 JavaScript 205 JavaScript 206
12. 207 208
209 210 211 213
216 216 2 ] 8 220 221 225 ... 227
SQLite 229 13. 231
231 2 32 , 233 2 34
236 238 system() 239 () 239 passthru() 2 40 240
FTP 241 FTP 241 2 42 FTP 2 42 FTP 243
244 245 246 246
14. 250 250 251
10
PEAR 254 PEAR 255 PEAR 256 PEAR 258 PEAR 259
V. 263 15. ,
265 265 266 266 266 , 266 267 267 268 268 269
16. Web- 270 Web- 270 270 Zend 270 Web-pecypc Builder 271 Web- Black Beans 271 PHP 271 Web-pecypc PHP Dev Center 271 Web- PHPMac .com 271 271 Web-pecypc SourceForge.net 271 Web- 272 Web- 272
VI. 273 . 275
Unix/Linux 275 Unix/Linux 276 Unix/Linux 276 Apache 278
280 280 281
Unix/Linux/Mac 283 Apache Unix/Linux/Mac 284
Windows 286 CGI 287 289 Web- Windows 290
11
. 293 2* '3 297 297 HTTP 3(12 3(12 3(14 305 308
3.0
12
OJ atftftofie (Janet Valade) ,
Linux Web-. . Web-
Web- , .
, ,
,
, .
/ , .
'BUatoqafiHOCfHU .
, , ,
. ,
,
.
.
/ , , .
, ,
.
, .
.
, Web- . , , ,
, .
, ,
.
. :
E-mail: i n f o @ d i a l e k t i k a . c o m WWW: http://www.dialektika.com
:
: 115419, , / 783 : 03150, , / 152
, , . ,
, .
. .
, Web- . .
Web-. , . ,
. , .
.
, ,
.
.
.
*
: . .
, . ( .) . , .
. ,
Web, , . ,
, . , ,
.
, ,
. . -
, . \ , ,
.
, ,
. :
, ,
.
, . ,
echo !, 2 .
echo 3, 127
, ; . ,
. ,
echo !, 2, ... ,
.
nftec/ , , ,
, , , , .
, .
, , ,
, .
, ,
. .
, Windows 95 , Mac OS 9. Windows 98 , .
Web-, HTML (HyperText Markup Language). , . .
HTML, , Web-. , HTML, , Web-. HTML.
, Web-, , Web-. , Web-, Web.
- .
, . ,
, .
. , ,
.
Gnfiifiantjfia ,
, .
I. " " . ,
.
II. " " .
. . ,
, .
17
III. " " , ,
- .
IV. " -" ,
. , Web-, .
,
.
V. " " ,
, . Web-, .
VI. "" .
, .
/, ,
.
-
. ,
.
, .
! , (, , ) * - .
.
, -
^^?- .
18
I
v4w , (. ^obtpwiHHO Hi 006 .
3 / . .
, .
.
.
-
.
1
:
>
>
>
, . ,
. ,
, . , ,
.
, .
, .
, Web-. , ,
Web-. . , ,
. ,
, .
,
.
. Web-. (Rasmus Lerdof), PHP Web- (Personal Home Page ). , .
Hypertext Preprocessing, Web- .
.
/ Web-. HTML-, , Web-, .
S . , , , .
S . .
, HTML-.
U
S . PHP , Windows, Linux, Mac OS Unix.
S . Web- (www.php.net), , , FHP, Windows . news . php .ne t .
S . , .
/ . , -
, .
, .
PHJ* .
,
, ,
"" .
, , -
Web- . W:b, ,
Web-. , Web-, .
Web-. , Web- (www.php.net/usage.php), 11 . Web- Yahoo!, , , , .
Web- Web- , ..
. Web-, . ,
, . Web- . , Web-, .
Web- . , Web- . , -
Web-. , - .
(server-side language) . , (, Web-.!). Web- JavaScript. , JavaScript, ;-. , JavaScript (client-side language). Web- " Web" .
22 I.
,
HTML- Web-. , "" Web-. , .
,
Web-. S HTML-.
HTML- , . S .
, ,
.
J Web-. Web-, Web-.
Web-. , ,
. , -
, , ,
. ,
, .
, JavaScript , . , JavaScript . , ,
. JavaScript Web-, , . JavaScript 11.
. ,
- .
. -
. ,
: , .
:
/
dBASE Informix Ingres Microsoft SQL Server mSQL MySQL Oracle PostgreSQL Sybase
1. PHP 23
, filePro, FrontEase InterBase. ODBC (Open Database Connectivity), , Access DB2.
Web-, . PHP- . ,
Web-. , .
: , , : . ,
.
, , , ,
. ,
, .
Web- . , - Web- , , , , .
. , -
,
. .
:-, .
, d i r I s .
. ,
, , ,
.
-
, .. , *-
, .
, , ? PHP-, , Peri, , (shell scripts). , ,
.
/ PHJ* . ,
.
, .
24 I.
, ,
, , .
.
Web-, Web. , .
,
CLI. . ,
.
, Perl . , CLI, 3.
World Wide Web
, " ", World Wide Web j (WWW). Web , Web-. j Web- . Web- " j ", , URL (Uniform Resource Locator ). URL Web- , ) www.mycompany.com/' w e l c o m e . h t m l . Web-, 1 URL- . .
| 1. Web- Web . 2. , URL-, . 3. Web-, , . 4. Web- HTML-, URL- . 5. , Web- , ( ] , Web- .) 6. Web- HTML- Web-.
Web Web-,
Web-. , Web- Web-. Web- , Web- " ". Web-.
Web- (embedded scripting language). , HTML- HTML, .. HTML-, , , .
, Web- , PHP- . .php .phtml, Web- .
, Web- HTML- . , PHP- , .
1. 25
HTML, FHP , , .
. , .
:
IM.
echo , HTML-. :
3, HTML-,
.
, , Web- . PrIP Web-, . Apache Software Foun ia-tion, Web- Apache. Microsoft IIS/PWS, iPlanet ( Netscape Enterpiise Server) Web-.
Web- , Web- " ",
I Web- (Web server). Web-, | Apache. , | www.ne tc ra f t . com www.secu r i t y space . com/s_su rvey /da ta / , 60% Web- | Apache. Web- Apache , .. j , . ! Linux Mac OS X. Wet-| Apache h t t p d . a p a c h e . o r g . j Apache Software Foundation, : i Web- Apache. ; Web-. Internet Information Serve r ! (IIS), 30% Web-. Microsoft, IIS- j Windows.
. Web- Zeus, NCSA Sun ONE!. | Web- 2,5% Web-.
fne .
,
, Microsoft, Macromedia Adobe, , .
,
. ,
- , ,
,
, .
, , ? .
26 I.
. ,
, .
,
, . ,
, , .
, , ,
, .
.
,
, .
Web- . ,
. ,
.
, .
(announcement mailing lists), . ,
, . , .
, , Web- w w w . p h p . n e t / m a i l i n g - l i s t s . p h p .
5 , 5,
. ,
. Zend ( ), , , .
5 - , 4. . -
, (exceptions). , - , ,
5. (- 9.) 5 .
Web php-cg i . PHP CLI php, php. exe Windows. , . 5 php. exe, .
5 MySQL 4.1 . , MySQL 4.0 MySQL 4.1 . 5 MySQL 4.0 .
5 SQLite, (flat files).
, .
, , ,
.
.
1. 27
S 4.3.1. , 4.3.0. Web- 4.3.0 .
S 4.3.0. CLI,
( ). ,
.
S 4.2.0. r e g i s t e r _ g l o b a l s Off. , , On , , . ,
.
S 4.1.0. . , ( 6), - . 4.1.0 , , , $HTTP_POST_VARS.
, 5. Web- ! [ .
.
28 I. PhP
2
/ ... > Web- > Web- > -
, , -
+S * Web-. , Web-, Web-. ,
Web, . , Web, " ".
Haanfiouica Web- 1, Web- Web-
. , Web- Web-. Web- . , ,
. Web- . .
S Internet. S ,
, Web-. S
, .
,
, , ,
. Web- , . ,
Web-, . Web-, , - ,
,
Web-. , Web-, . ,
, Web- - . Web-,
- ( Web-
, Web-). , , Web- Web-, , Web-.
Web-. S , .
Web- . ;- , -
. - .
S . Web, HTML . Internet, Web-, .
, .
Web- . S .
Web- . S . ,
.
S . , , . , , -
, , , ;;, - .
S . Web-, . -
. ,
. ,
.
Web- Web-, - ,
Web-. - , - . -
Web-, . Web-.
Web- Web-.
S Web-. , Web- . Web-, Web- , - . ,
, .
S Web-. , . FTP . FTP (File Transfer Protocol) : .
30 I.
Web-. (user ) .
S , . URL, . Web- , , URL- . index.htm index .h tml , d e f a u l t . h t m . , , .
S . Web- .
.php .phtml, .
. ,
.
Web- . Web-, , , . , ,
, . ,
, ,
. ,
, .
, ,
. ,
.
, Web-, .
Web- , . .
, .
, ,
. , ,
Web-. , , 2 , , , 10 .
,
( ). , . ,
, .
, 3. 4.3.1 2003 , , 4.3.1 ,
. ,
5.
2. 31
\
.
S . , . ,
Bai jero . , , , .
S . , , - Web-. , - . , -
Web- , ocof ioro . -
, .
, ; .
S . , .
. -
. ,
.
, < .
S . , Web- . , (.
; ,
. , , Good Stuff , JanetsGoodStuff. JanetsGoodStuff . com, , JanetsGoodStuff.Web-hostingcompanyname.com, JanetsGoodStuf f .Web-hostingcompanyname . com/~GoodStuf f, - . -, ,
.
/ . :.
. , .
, , .
. (/) ? - ( ) .
.
Web- . , .
.
. ,
JanetGoodStuff.com, me@JanetGoodStuf f . com.
32 I.
.
Web-. , ,
, MySQL PostgreSQL, Web-, FrontPage, ,
..
.
Web- , , Web- ..
S . (backup) Web- , . ,
.
,
- .
Google "web-hosting" 4 . , .
, ,
.
, ,
, .
*j " " .
IP-. 0 255, , 1 7 2 . 1 2 . 2 0 4 . 2 .
IP- , . , IP- , , a m a z o n . com, www. i r e . g v mycompany . com. , , (domain name). , , . , . ,
, t h o r . m y c o m p a n y . c o m , (subdomain) mycompany . com. URL-, Web-, . URL- , , (, amazon.com) , . ( j a n e t . v a l a d e . com). , Web- ( i n d e x . h t m i n d e x , h t m l ) . , j a n e t . v a l e d . c o m / l i n k s . h t m l . , Internet. , .
, .
. ( ), .
35 . , .
2. 33
Web-, . Google " " 2 , i , . ,
. Web- , who is . Allwhois ( A l l w h o i s . c o m ) Better-Whjis j
i (betterwhois.com).
. PHP- .
. ,
,
marc . thea imsgroup . com/.
Web- Web- , -
.
. Web- , , .
.
Web-, , .
1. . 2. Web-. 3. . . , -
, Web- ( ) . "", 9- , "". Web- , Unix Linux, Windows MacOS.
Web- , , Web-
. , , Apache, .
S . ? S . Apache
Windows, Linux, MacOS, FreeBSD Unix.
S . , www.netcraft.com/survey www.securityspace.com/s_survey/data/, 60% Web- Apache. . ! ,
.
34 I.
f . Apache , .
.
/ . Apache, .
/ . , Apache SSL. SSL Web-, . , ,
Web- , , .
.
Apache Linux. , Apache . Unix Apache , (binary) ( ). Windows, ( Apache Windows NT/2000/XP, Windows 98/).
, Apache 1.3.27. Apache 2 , . www. php. n e t . Apache, , ,
h t t p d . apache . org. Web-. Microsoft
IIS (Internet Information Server), Internet: 27% Web-. Sun iPlanet ( Netscape Enterprise Server), 5% .
.
Linux. Mac OS X . , , .
PHP-. .
I S Linux/Unix/Mac. f ind / -name "php*" S Windows. ( ^) php*. PHP-, , .
Web- . , Apache, .
www. php. ne t . .
2. 35
- , , *
. , ,
.
| S . -, . ,
, . : ODBC, MySQL, Oracle, MS SQL ;- . ,
SQLite, XML, COM, FTP .., .
, , ,
.
,
. ,
. ,
, phpinf () , , . ,
. ,
. .
S . . .
php-cg i -v , , 5, : php - v , ,
p h p - c g i . e x e ( php.exe). :
5.0.0 ( cg i - f cg i ) , Copyright () 1997-2003 The PHP Grotp Zend Engine v2 .0 .0 . Copyright (c) 1998-2003 Zend Technologies ,
. ; www. php. ne t /downloads . php.
,
.
1. , . .
Apache h tdocs . , Web-. IIS lnetpub\wwwroot. Linux /var/www/html. Web- , Web- - , ,
. ,
.
36 I.
2. t e s t . php : TecpoBaHne PHP HTML
3. URL- t e s t . p h p , . 2. URL h t t p : / / w w w . m y c o m p a n y . c o m / t e s t . p h p . Web-, ,
URL- l o c a l h o s t / t e s t . php .
A^ 'g^N , Web-, .
:
HTML
,
. ,
, .
p h p i n f ( ) . , ,
. p h p i n f () . - ,
.
S HTML. .
S . / Web-.
,
, ,
. .
,
, . ,
,
p h p - c g i - v
php - v
2. 37
, , , [. , , . 3.
, ,
. 1. Apache h t t p d . c o n f
DocumentRoot ":/Program Files/Apache Group/Apache/htdocs" Apache, Web-. . Web-)
j a n e t . v a l a d e . com. , , -
, .
, .
.
S Web- , IP. , h t t p d . conf AddType a p p l i c a t i o n / x - h t t p d - p h p .php
S Apache, . php. ES , , .
S h t tpd . conf , . ,
.
S IIS, php. i n i : cgi.force_redirect =
S php. i n i . , , . -
1 0.
, , , -
Web-. , , , : .
- , .
marc . thea imsgroup. com/. , - ) , .
. .
S , . S , . S . S , Web-.
, . 18- .
38 I.
Hactflftoutca THJ* qj& cu^Haftuetf
PHP . Web- . CLI , Web-. .
, CLI . , .
. CLI php.exe, a PHP CGI p h p - c g i . e x e . ( 5 php. , . CLI / c l i . ) , CLI - . .
S Linux/Unix/Mac. : f i n d / -name "php*"
S Windows. ( ) php*.
, , - , CLI, , , : php -v
, cg i c l i : 5 . 0 . 0 ( c l i ) ( b u i l t : Jun 15, 2003 2 3 : 0 7 : 3 4 )
, ( c l i ) . Web, ( cg i ) . CLI. , , , .
CLI, . , Web. , CLI.
Web-, , PHP- Web CLI. , .. 5.0.0 Web, , CLI 5.0.0. Windows p h p 5 t s . d l l , . , -
php5 t s . d l l . ( .)
Hacfnftoutca THJ* .
. ,
. -
2. 39
php. i n i . , .
, php. i ai. , . -
, . , -
(, - ), php. i n i . 1 , .
,
. ,
, .
cfteqctn , PHT-cu^Hafiuee
PHP- . , .
v i , Notepad WordPad. , , .
(Integrated Development Environment). ,
Internet . .
S Arachnophilia (www.arachnoid.com/arachnophi l ia / ) . - Java .
S BBEdit (www.barebones . c o m / p r o d u c t s / b b e d i t / i n d e x , shtml). .
BBEdit 179 . BBEdit Lite. , . BBEdit Lite TextWrangler, 49 .
S EditPlus (www.editplus.com). Windows. EditPlus - , 30 .
S Emacs (www.gnu.org/software/emacs/emacs.html). Emacs Windows, Linux Unix .
f HomeSite (www.macromdia.com/software/homesite/). HomeSite Windows 99 .
V HTML-Kit (www.chami .om/html-k i t / ) . Windows, .
S vim gvim: (www.vim.org/). v i Windows, Linux, Unix MacOS. gvim , Windows .
(Integrated Development Environment ) .
.
, .
S . .
S . Web-, .
S . . S FTP. FTP
. , Web-, .
S . ; , .
S . () Web-.
2. 41
,
. , .
,
.
.
.
| S Dreamweaver MX (www.macromedia.com/dreamweaver). Windows. , Web-, COOTI - . Dreamweaver HTML- . HomeSite, , .
Dreamweaver 399 . S Komodo (www.activestate.com/Products/Komodo/) . Komodo
Windows Linux. ' , Perl, Python, . 29,95 295 .
S Maguma (www.maguma.com). Maguma Windows. Apache, PHP MySQL Windows. , Maguma Studio Desktop Maguma Studio Enterprise, / ,
. Maguma Studio .
S PHPEdit (www.phpedi t .net /products /PHPEdi t / ) . - Windows.
/ Zend Studio (www. z e n d . c o m / s t o r e / p r o d u c t s / z e n d - s t u d i o . p h ? ) . Zend Studio Windows Linux. ,
Zend . Zend Studio 195 .
Web- , ,
p h p e d i t o r s . l i n u x b a c k u p . c o . u k . 111 .
42 I.
3
/ &... >
> HTML >
>
, ,
. PHP- . ,
- .
.
- , .
1, , , . Web- , Web-. , Web-, . ,
Web-. , , Web- , .
, . ,
, , .
, .
, . , ,
.
onefiatfiofioe !PH!P , .
echo. . echo: echo "";
echo , (""). .
echo (simple statement). (;). , ; ( , ). . , , . -
.
,
. :
Parse error: expecting ' , ' " or ' ; ' ' in f i l e .php on l i n e 6
, , .
, ,
, .. .
,
. aHanj - . , ,
. ,
. 553 , . ,
, .
, >- .
. , .
,
({ }). , . , i ;- . : i f (_ == ) {
; ; ;
} ,
. , .. _ = , ,
. ( , , ).
, (, , i f ) , (complex statement). , .
.
, , .
. -
, , . , ci ;-, , .
,
.
.
/
, .php. , , . ,
.
44 I.
Web- - HTML- Web-. Web, , . , .
| HTML ( . h t m l . h tm) , Web- : . , ,
HTML Web-. | ( . php ) , Web- ( ). .
1. Web- HTML, .. i HTML, .
| 2. HTML , | ().
| 5. ?>, Web- HTML , . 1.
HTML- Web-
HTML- Web-. . php, Web- . ( Web- ,
, .php4 .phtml. .php , .)
HTML , , HTML. :
, .
,
php . i n i .
. , Web- , , PHP- , .. .
3. 45
, ,
Web- . ) ICT- , Web-. , , (.. ) HTML . .
, HTML . . php:
Web- . php . HTML
.
Web , .-
Web. CLI (Command Li ie Interface ). CLI , Web- ( CGI), . CLI
CLI , , ; , Unix Linux. , Windows, ( ). CLI Linux/Unix, Windows. ( .)
:
46 I.
i 4 > 4^- Linux/Unix : (shell script) Perl 1 Linux/Unix, PHP- . ,
CLI, :
#! /usr/bin/php 1 \ | , , / u s r / b i n / p h p . Windows ,
. # ! / u s r / b i n / p h p Linux/Unix, | , Windows .
I . , | j , . , , ' | , t e s t . p h p , j tes t .php ; : /mystuff / tes t .php j He , , ,
.
t e s t c l i .php, ( , , CLI): php t e s t c l i . p h p
:
/usr/local/php/cli/php testcli.php Windows,
.
StartoPrograms^Accessories^Command Prompt (^^-
1^ ).
CLI CGI . S HTTP. CGI
Web-, , HTTP (, Web- ). , CGI : Content-type: text/html X-Powered-By: PHP/5.0
,
Web-. , CLI HTTP, :
3. 47
S . PHP CGI HTML, , , .
PHP CLI . / argc arirv.
argc a rgv ( ). CGI, CLI , . CLI 31. ( a rgc a rgv 5.)
(). , -v . : php -v
. 3.1 .
3.1.
- php . i n i . CLI php. i n i , - / u s r / l o c a l / p h p / c l i / p h p . i n i ( php. i n i .)
- . , php - / m y f i l e s / t e s t c g i . p h p - h - i , p h p i n f ()
(. 2) - 1 , - . ( 14.) - , .
, php - r ' p r i n t ( ' ' ) ; ' - v
/ nefi0oio / THJ* , >
, . HTML, , , :
HTML-nporpaMMa 3, !
, :
, !
48 I.
.
, HTML PHP- , ! CueHaprai PHP
, HTML.
, , ..
File^OpenO Browse (^). URL- (. 2). , , ,
URL-.
:
echo, , (" "). ,
:
3, ! PHP- , HTML-.
, , ,
( , View=>Source ( HTML-)).
2)1
S _ (, 1 250) . 5.
S -. ( 5 , .)
S echo . S echo (,)
.
S , .
. 3.2 echo.
3.2. e c h o
echo 123; echo ", !"; echo ",","!"; echo ",", " ","!"; echo , !;
echo '', !';
123 3 , ! ,! , !
,
, !
, , !, , HTML, . ,
1. , Web-, , , . HTML-, , -.
2. Web- HTML- Web-. HTML- "" . , , Web- , .
echo,
HTML-, , .
2. , Web-, , < /> HTML :
, !
. ,
Web-, HTML-. :
3, !
echo , .
\. : echo "3,\ !"
\ echo , . Web- . : echo "3, !"
.
. 3.3 echo : Web-. echo. (.. , ), , .
3.3. -
echo
echo ", !"; echo ","; echo "!"; echo ",\!";
, ! , !
, ! ,!
, ! ,
! ,! ,
! echo ",\!"; , ,
! !
echo ",!
. ,
, echo. , , .
echo. , ,
. ,
3. 51
\! . , \ (.. ), .
\. HTML- . , , , .
, HTML- ; - . -,
, HTML Web-. ,
. \ HTML- . ,
.
, . ,
.
, . Web, HTML. , Web, : .
2)//{1, .
, . ,
, .
, ,
. , , i . , -
, , .
(comments) , . .
. :
/* */
(/*), * / .
,
, . :
/* : hello.php : ", !"
Web- :
: 02/01/03 : 03/15/03
*/ .
(#) ( / / ) . . :
52 I.
# 1 / / 2
# / / , . .
:
echo ""; / / , HTML-,
, .
,
:
/* , 18 */ /* */ /* */
, .
:
################################ # # ################################
, . ,
, : .
( ) . , , echo, , , . ,
.
, .
. :
/* /* */
*/ , /*,
, */. /*. , , /* */. */, , .
3. 53
II
*\, * *W", >" u W & & tttwu. ^
W , W W .
/3 /
. ,
, .
,
, .
4
Sfflou ... >
>
>
>
>
(variable) , . ,
. , $ 21. .
Web , HTML, . Web- , " , !"
,
. , .
iLueHa nefieutettHbtx , , .
, $var l , $var2, $var3 .. , , , -
,
. , ,
, .
, ,
$f irstName, $directory_name $DateOfBirth. .
| S ($). .
S . S ,
.
/ . .
f . $ f a v o r i t e c i t y $ F a v o r i t e c i t y . : $ F a v o r i t e C i t y , - ,
$ f a v o r i t e C i t y .
: $_name $first_name $name3 $name_3
:
$3name $name? $first+name $first.name
, -
, . , -
, .
.
.
:
$first_name $firstName
. .
.
otiiorffiajtcetiue nefieueHHbtx
.
, .
. , \. , , . ,
, .
,
.
.
, (=) . , :
$ = 21; $price = 20.52; $temperature =-5; $name = " ";
58 //.
, , ,
, . ,
, .
, ,
.
5. , ,
. , , :
$color ""; $color = "";
" ".
$co lor " ". ,
:
$namel = ""; $name2 = ""; $favorite_name = $name2;
$ favor i t e_name "".
, . ,
:
$c i t y = ""; , .
5.
p r i n t _ r . : $today = ""; print_r($today);
.
echo.
$ = 21 ; echo $age; 21.
echo , p r i n t _ r . echo . , . ,
, $name . HTML- : , /p>
Web- : ,
,
. , ,
$, : echo $aeg;
4. 59
:
Notice: Undefined v a r i a b l e : aeg in c : \ tes tvar .php on l i n e 5 (Notice) ,
. ,
. .
, ( ). , ( 7). , . ,
@. , , : echo @$aeg,-
, . ,
, , echo . , . , i ,
. ,
, .
, .
,
.
,
. ,
, Java .
/ [1, , 3, Web- ,
echo. , , . 3 :
,
", !": CueHapmi nepeMeHH0H
, Web- , !
4S2&
'"-^
60 II.
,
-. 5 , 5 (, , ). , : CiJ,eHapira nepeMeHHOH HTML
URL- , Web- :
, ! HTML , !
' noqfioduo orf onefiatnofiax 3, echo :
echo _1,_2,_,... . ,
-:
:
$f i r s t_name $last_name. , :
, ,
. . 4.1 echo, , . echo : $number = 12 3; $wordl = ","; $word2 = "!";
,
, .
.
4. 61
. 4.1. echo echo
echo $number; 123 echo $word,$word2; ,! echo $wordl," ",$word2; , ! e c h o $ w o r d l $ w o r d 2 ; ,
.
echo "$wordl $word2 "; , !
echo , .
. , ,
$type = "b i rd" ; echo "Keep the $typecage clean";
:
Notice: Undefined v a r i a b l e : typecage in testvar.php on l i n e 6 :
Keep the c lean , echo
:
echo "Keep the {$type}cage c lean" ; :
Keep the b i rdcage c lean
/ / ,
. , ,
, .. .
, , $ c i t y -. : $name_of_the_variable = " c i t y " ;
, ,
.
$$name_of_the_variable = "-"; . "
. , -
, $name_of _ t h e _ v a r i a b l e : $c i ty = " ";
$name_of _ t h e _ v a r i a b l e . . >
,
.
, .
6 7.
62 II.
, ,
, ,
. :
$Reno = 3 60000; $Pasadena = 13 8000; $cityname = "Reno"; echo " $cityname - ${$cityname}"; $cityname = "Pasadena"; echo " $cityname - ${$cityname}";
:
Reno - 360000 Pasadena - 13 8000
, echo . .
$ = ; $. ,
$ 0. , $ . , $ . $ echo, . ( .)
: unse t ($age) ;
$ . echo, "undefined v a r i a b l e " ( ). , : unset($age, $name, $address);
Padotna c icoHctiiaHtnaMU . .
; . , .
"",
.
d e f i n e . :
define (" _" , " _") ,-, weather, :
def ine("weather" , ""); weather .
4. 63
, ,
. , ,
. ,
,
. .
.
INTEREST .01: define("INTEREST",.01);
, .
, echo. , PHP-, . ECHO, , . , \ e c h o ECHO;
.
echo, , echo.
: and echo l i s t as e l s e new break empty or case eval p r i n t c l a s s e x i t r equ i r e const for r e t u r n cont inue foreach switch dec la re function use de fau l t g lobal var d i e i f while do include
, , < >- ,
. , ,
- . , .
, { -, $. , ,
.
, -
, .
. ,
PR0DUCT_COST , 20.50.
. - ,
,
. ,
. .
64 II.
,
- .
, ,
, . ,
1.52, : , 40 000
50 . , 50 . , , ,
, 1.52 50 . , .
,
. :
$ r a t e , 50
. , .
, .
, 1.53, : $ ra te = 1.53;
. $ r a t e . , 40 000 2 , , 50 . , - ,
. , - : $ i n t e r e s t _ r a t e = 20; $ ra t e = $ i n t e r e s t _ r a t e - l ; $amount = $pr inc ipa l * $ r a t e ;
$ r a t e . 1.52, , 19, . , .
, : RATE, .
RATE = 20; . ,
.
4. 65
, 1. 53, :
, , :
define("US_TO_CA", 1.52); , ,
, , ,
, ,
.
,
, .
, .
p r i n t _ r : print_r(US_TO_CA);
echo: echo US_T0_CA;
echo, . ,
, . ,
, .
, , : echo " $",US_T0_CA;
:
$1.52
, > . , LINE , FILE . ( -
.) , echo FILE ; :
c:\program f i l e s \ apache g roup \apache \h tdocs \ t e s tva r2 .php . ,
E_ALL E_ERR0R . .
66 II.
. ( , p h p . i n i , .)
, p h p . Lni , :
e r r o r _ r e p o r t i n g = E_ALL; d i s p l a y a l l e r r o r s , w a r n i n g s and n o t i c e s ( , )
. 1 ' .
.
,
E_ALL, . , ] p h p . i n i . , . ,
.
p h p . i n i , , . ,
. p h p . i n i 1 .
E_ALL , , ; E_NOTICE , . :
e r r o r _ r e p o r t i n g = E_ALL & ~E_NOTICE E_ALL ,
. ~E_N0TICE , .
.
, , .
, , .
, .
E_ALL E_NOTICE . p h p . i n i . IP.
.
1 ,
.
. .
, p h p . i n i d i s -p l a y _ e r r o r s = On On Of f.
, -
p h p . i n i Web-.
,
error_reporting() ;
>
68 //.
, .
, ,
, :
error_reporting(E_ALL); , php. i n i
E_ALL. , .
, php. i n i ,
error_reporting(E_ALL & ~E_N0TICE) ,- ,
.
error_reporting(0) ;
. , ,
,
.
. 0, ,
. , .
.
.
.
.
php. i n i , . php. i n i
log_er ro r s = Off Off On. ,
. ,
; e r ro r_ log = _ ,
. _ ,
. ,
error_log = c:\temp\php_error_log , ( Windows
). , : \temp. . , .
php . i n i , Web-.
4. 69
(D
.
. ,
, .
.
, ,
. , . , ( , $ h e i g h t _ o f _ d o o r , $he igh t_of__house , . , . .
, :
i f ( $ h e i g h t _ o f _ d o o r > $ h e i g h t _ o f _ h o u s e ) {
t r i g g e r _ e r r o r ( " I m p o s s i b l e c o n d i t i o n " , E_USER_ERROR); }
i f 7. E_USER_ERROR , .
I m p o s s i b l e c o n d i t i o n , - . , :
F a t a l e r r o r : I m p o s s i b l e c o n d i t i o n i n d : \ t e s t e r r . p h p on l i n e 9 , ,
, .
E_USER_ERROR E_USER_WARNING E_USER_NOTICE, - .
,
,
, , . ,
:
i f ( $ h e i g h t _ o f _ d o o r > $ h e i g h t _ o f _ h o u s e ) {
echo " "; e x i t () ,-
} $ h e i g h t _ o f _ d o o r $ h e i g h t _ o f _ h o u ,
e x i t () . .
,
e r r o r _ l o g (message , 3 , __) ,-, i f :
i f ( $ h e i g h t _ o f _ d o o r > $ h e i g h t _ o f _ h o u s e ) {
e r r o r _ l o g (" " , 3 , " / t e m p / e r r _ l o g " ) ; e x i t ( ) ;
} , $ h e i g h t _ o f _ d o o r
$ h e i g h t _ o f _ h o u s e , / t e m p / e r r _ l o g . 3
70 II.
. /temp . , .
.
, ,
e r r o r _ l o g . 1 e r r o r _ l o g : e r ro r_ log (" ", 1, "[email protected]");
,
PHP-. 13. , -
. ,
. , ,
. ,
.
. ,
.
. ( 8.) , ,
set_error_handler(_); , :
se t_er ror_handler (my_error_handler ) ; my_error_handler
8.
d ie , . 8.
4. 71
5
/ ... >
>
>
>
. ,
. ,
1+2. (+) -. ,
.
/ .
* (integer) ( ), -43, 0, 1, 27 543 8. .
-2 2 . S (floating point)
, 5 .24 123 .456789. (real number) (flc at).
S (string) , .
- .
S () (boolean) : TFUE () FALSE (). .
,
.
. .
--
, ,
. .
,
.
:
: T R U E () F A L S E (). . , $>$ TRUE (), FALSE (). .
" F A L S E " ( , ); 0; . ; ; , " " ; N U L L . TRUE. echo , F A L S E , 1 - . .
, .
8.
.
, : $firstNumber = 1 ; # $secondNumber = 1.1;# $sum = $firstNuinber+$secondNumber;
, ,
.
f loa t , . , .
.
.
.
(type casting). : $newint = ( i n t ) $ v a r l ; $newfloat = ( f l o a t ) $ v a r l ; $newstring = ( s t r i n g ) $ v a r l ;
, ,
, . , $va r l $newint i n t e g e r ( ( i n t ) ) .
.
. ,
. , $number 1. 8, $newnumber = ( in t ) $number $newnumber 1.
var_dump ($myvariable) ,-
5. 73
, $checkvar: var_dump($checkvar) ;
i n t (27), , $checkvar 27.
Padotna f l o a t i n t e g e r .
:
$intvar =3; $floatvar =9.3;
.
.
.
, (+) : 1+2
:
$varl + $var2 ;
. ,
. ; -
. : $varl = " 1 " ; $var2 = 2; $ t o t a l $varl+$var2;
$var l $var2 , $var l .
" 1" 1 .
, :
$varl = ""; $var2 = 2; $total = $varl+$var2;
" " , -
0. , 1 $ v a r l $var2 2. -IT . , : .
, ,
.
,
. :
74 II.
$varl = "2,000"; $var2 = 2; $total = $varl+$var2,-
,
. "2,000" 2, $ t o t a l 4.
. 5.1 .
5.1.
+
*
/ % ( ). ,
$ = 13%4 $ 1
. ,
:
$ t o t a l = 1+2*3+1,- ,
.
.
. , $ t o t a l 8. $ t o t a l = 1+2*3+1 # $ t o t a l = 1+6+1 # - $ t o t a l = 7+1 # - $ t o t a l = 8
.
. , : $ t o t a l = ( l+2)*3+l;
$ t o t a l 10: $ t o t a l = (l+2)*3+l # $ t o t a l = 3*3+1 # $ r e s u l t = 9+1 # $ r e s u l t = 10
. , (3+2*5) , . ,
.
" "
,
.
5. 75
1 : $counter $counter+l ;
:
$counter++; ,
$counter=0; $counter++; echo $counter;
$counter 1 ( ). : $counter - - ;
1 : "+=1". - . :
$counter+=2; $counter-=3; $counter*=2; $counter/=3;
$counter Hi. 2, 3, 2 3 .
. ( 8.) , , ,
. :
$rootvar = sqrt(91); $rootvar = sqrt($number);
91, , $number.
$upnumber = ceil(27.63); 28. ,
. :
$downnumber = floor(27.63); f l o o r () 27. :
, , ,
,
.
- ,
76 II. \
.
. , 10.00, 10. , .
number_f ormat () number_format(, __, "!", "2") :
S . , . ^ __. .
, 0 , , (.. ). I !, __ .
S . , - .
. ! , !.
S !. , . .
. 5.2 number_f ormat ( ) . 5.2. number_f ormat ()
$number
12321 12321.66 12321.66 12321.6 12321 12321.66
number_format($number) number_format($number, 2) number_format($number) number_format($number, 3) number_format($number, 0, number_format($number, 2,
12,321 12,321.66 12,322 12,321.600 12.321 12321.66
.
.
p r i n t f () s p r i n t f ( ) .
S p r i n t f () . S s p r i n t f .
p r i n t f () s p r i n t f () , . ,
" ".
5. 77
Parfo/na cfnfiOKcuiu (character) , . (1
) (character string) . , , , ..
. ,
, .
. :
. , :
$ s t r i n g = ", ! " ; $ s t r i n g = ' , ! ' ;
-~ ^ .'"' '"
| ^ * ^ \. neredoc-, ] , . : t : [ $_ -
, \ \ t , . \ . : $ s t r i n g = ", \"; echo $ s t r i n g ;
:
,
\ t . , $ s t r i n g = " 1 \n\tcTpc-Ka 2" ; echo $ s t r i n g ; , :
1 2
,
.
.
.
-
.
S , , " ", \ ', . ( .)
S , .
.
,
, . :
$name = ""; $outputl = "$name"; $output2 = '$name'; echo $outputl; echo $output2;
:
$name .
, ,
- . :
$ s t r i n g l = " \\ "; $s t r ing2 = ' \\ ';
5. 79
:
:
\\ ,
. :
$number = 10 ; $stringl = " '$number' ."; $string2 = ' "$number" .'; echo $stringl, "\n"; echo $string2;
:
'10 ' . "$number" .
, , , $ s t r i n g l $number , , . .
, ,
.
,
" ", .. . , ,
,
. ^
(backslash) (\). : $ s t r i ng = ' $ v a r l ' ; $ s t r i n g = " - \ $ v a r l " ;
:
i s $var l * , , :
$ s t r i n g = ' '?'; echo $ s t r i n g ;
, ; ('), . :
,
, . -
, ..
:
$string s ' 0\'?'; , , ,
", \.
80 II.
(concatenation). (.). :
$ s t r i n g l = ","; $s t r ing2 = "!"; $ s t r i n g a l l = $ s t r i n g l . $ s t r i n g 2 ; echo $ s t r i n g a l l ;
:
,! , (""),
.
:
. :
$ s t r i n g a l l = $ s t r i n g l . " " . $ s t r i n g 2 ; . =.
, : $ s t r i n g a l l = ","; $ s t r i n g a l l .= " !"; echo $ s t r i n g a l l ;
:
, !
.
( 8.) ,
, , ..
.
:
$ s t r i n g = t r i m ( $ s t r i n g ) # $s t r i ng = l t r i m ( $ s t r i n g ) # $ s t r i n g = r t r i m ( $ s t r i n g ) #
s t r_word_count () :
str_word_count("", ) : 1 2. 1
str_word_count () , . 2 , . ( .) , s t r_word_count () . :
$e t r ing " "; $numberOWords = s t r_word_oount ($s t r ing) ;
5. 81
$wordl = s t r_word_count($s t r ing, 1 ) ; $word2 = s t r_word_count($s t r ing, 2 ) ;
:
$numberOfWords = 2 $wordl[0] = $wordl[l] = $word2[0] = $word2[9] =
, 0 ( 1, ivor- ), 9. 6 .
,
, . 5.3. , , - .
. , -
echo , . :
$number = 4 ; echo " $number .";
:
4 . $number 4, echo
.
.
echo . " " number_f ormat () , . ^ -
. p r i n t f () s p r i n t f () , , .
p r i n t f () s p r i n t f () : printf("", $_1, $__2, ...); $newvar = sprintf("", $_1, $_2, ...);
p r i n t f () , s p r i n t f () . , . 5-, $_. , : $newvar = spr in t f (", !");
, ,
! $newvar. , :
--1?nboys = 3 ; $ng i r l s = 2; pr in t f ("%s %s ", $nboys, $ n g i r l s ) ;
82 II.
5.3.
str_repeat("", ) s t r _ r e p l a c e
"") ("", " s t r c h r ( "" , "") s t r i s t r {"", "") s t r l e n ( "") s t r p o s ("", " s t r r c h r {"", "") s t r r e v l "") s t r r p o s {"", "" strtolower ("") strtoupper ("") s t r t r ("", " 1 "2") s u b s t r {"", 1, 2) s u b s t r _ c o u n t {"", "sub") s u b s t r _ r e p l a c e ("S", "", , I) u c f i r s t ( "") ucwords ("")
b
,
s t r c h r ( ) ,
) s t r c h r ( ) ,
)
, 1 2 , J 2 sub
s 1 , ,
$ = str_repeat("", 5) ; $ = "abc abc"; $s = str_replace("b", "i", $a); $str = "aBc abc"; $sub = strchr($str, "b"); $str = "aBc abc"; $sub = stristr($str, "b"); $n = strlen(""); $str = ""; $n = strpos {$str, ""),-$str = "abc abc"; $sub = strrchr($str, "b"); $n = strrevl"abcde"); $str = "abc abc"; $n = strrpos($str, "be"); $str = strtolower(""); $str = strtoupper(""); $str = "aa bb cc"; $new = strtr($str, "bb", "xx"); $sstr = substr("", 2, 4); $str = "abc ab abc"; $s = "be"; $n = substr_count($str, $s) ; $s = "abc abc"; $t = substr_replace($s, "x", 2, 3); $str = " "; $str2 = ucfirst($str); $str = "aa Bb cc"; $str2 = ucwords($str) ;
$x = $s =
$sub
$sub
$n = $n =
xxxxx
aic aic
= be
= Be abc
6 2
$sub = be
$n = edeba $n = 5
$str = $str = $new = aa xx cc
$sstr =
$n = 2
$ t = abxbc
$str2 =
$str2 = Aa Bb Cc
%s , p r i n t f () . , 3 2 . % p r i n t f , . : %-.
.
S (%) . S . -,
( ). ( ), 0 , .
- 01 0001. S (-).
, ( ). S . >ix
, -
. , 5, 0, 1, 00001
S . . , .
S . s (string). f (float).
s p r i n t ( ) . $money = 30; $pet = "Ki t ten"; $new = sprintfC'It costs $%03.2f for a %s.\n", $money, $pet); $new2 = sprintf("%'.-20s%3.2f", $pet, $money); echo $new; echo $new2;
.
I t cos t s $030.00 for a K i t t e n . Ki t t en 30.00
, $money f- 3.2 f ( ). : , $new $new2. $new $money 0, $new2 .
$pet $new2 ' . -20. 20 , $pet. K i t t e n , - (' .), .. 14 (.). (-) , K i t t e n . .
. , , : 12 . :i, 1 234.55. echo :
84 II.
12.3 1 234.55
number_f ormat () :
12.30 1.00 234.55
p r i n t f ( ) : p r in t f ( "%5.2f \n" , $numberl); p r in t f ( "%5.2f \n" , $number2); p r in t f ( "%5.2f \n" , $number3);
:
12.30 1.00 /
234.55
%5. 2f \. . S %. . / 5. , .. ,
. 5, - ( ). ,
.
S 2. . S f , . / \. .
( $), s p r i n t f () . : $newvariablename = spr int f ("$%.2f" , $oldvariablename);
$oldvar iablename, $newvariablename. :
$pr ice = 25; pr in t f ("$%.2f" , $ p r i c e ) ;
:
$25.00
qounbt [ .
.
timestamp . , .
5. 85
. 5.4
: am ( ) : A M ( ) , 1 1970 , 00:00:00 GMT
am
AM
1056244941
timestamp timestamp
t ime (): $today = t i m e t ) ;
$today = strtotime("today"); timestamp
mktime () . : $importantDate = mktimefh, m, s, mo, d, y); h ; m ; s ; mo ; d ; . , , 15 2004 , :
$importantDate = mktime(0, 0, 0, 1, 15, 2003); timestamp
. ,
: $importantDate = s t r t o t ime(" Janua ry 15 2003");
s t r t o t i m e () . S : 12 . S : 7 . S : yea r (), month (), f o r t n i g h t (
), week (), day (), hour (), minute (), second (), am ( ), ( ).
/ : ago ( ), now (), l a s t (), nex t (); t h i s (), tomorrow (), y e s t e r d a y ().
S "" "". S . S : , gmt (Greenwich Mean Time
) pdt (Pacific Daylight Time ). - . :
$importantDate = s t r to t ime(" tomorrow") ; # 24 $importantDate = s t r to t ime("now + 24 hou r s " ) ; $importantDate = s t r t o t i m e ( " l a s t Sa turday") ; $importantDate = s t r tot ime("8pm + 3 days" ) ; $importantDate = s t r t o t i m e ( " 2 weeks ago") ; # $importantDate = s t r t o t i m e ( " n e x t year gmt"); # $importantDate = s t r t o t i m e (" tomorrow 4am"),-
5. 87
IHO . , $importantDate , "" : $ today ( ). : $timeSpan = $today-$importantDate;
.
:
$timeSpan = ( ($ today-$importantDate) /60) /60;
88 II.
6
/ ... >
>
>
>
>
,
. , $FordInf , , .
, .
, .
, ,
.
fiarfoina .
, , .
. ,
, . ,
$customers , . $customers $ c u s t o m e r s [ 1 ] = " ";
$customers " ". :
$customers[2] = " "; $customers[3] = " ";
$customers : " ", " " " ".
- (key/value), .. $arrayname['1'] = !; $arrayname['2 ' ] = 2; $arrayname[' ' ] = ; .. .
(key) (index).
.
$ c u s t o m e r s 1, 2 3. . ,
:
$ c a p ' i t a l s [ '' ] = ""; $ c a p i t a l s [ ' T X ' ] = ""; $ c a p i t a l s [ ' O R ' ] = "";
,
. ; $ s t r e e t s [ ] = ". " ; $ s t r e e t s [ ] = ". "; $ s t r e e t s [ ] = "7 - ";
,
0. , :
echo " $ s t r e e t s [ 0 ] " ; :
.
0, .
1, 0.
$ s t r e e t s = ( " . " , " . ", "7 - ") ; ,
. , , , , [ 12, 0, $ s t r e e t s = a r r a y ( 1 2 => ". " , " . ", " 7 - ") ;
:
$ s t r e e t s [ 1 2 ] = " . " ; $ s t r e e t s [ 1 3 ] = " . "; $ s t r e e t s [ 1 4 ] = " 7 - ",-
. ,
:
$ c a p i t a l s = a r rayC 'CA" => "", "" => "", "OR" => " " ) ) ;
.
.
.
, ,
.
.
,
. :
$ y e a r s = r a n g e ( 2 0 0 1 , 2 0 1 0 ) ; :
90 II.
$years[0] = 2001 $years[l] = 2002
$years[8] = 2009 $years[9] = 2010
$ r e v e r s e _ l e t t e r s = r a n g e ( " z " , " a " ) ; 26 : $ r e v e r s e _ l e t t e r s [ 0 ] = z $ r e v e r s e _ l e t t e r s [ 1 ] = $ r e v e r s e _ l e t t e r s [ 2 4 ] = b $ r e v e r s e _ l e t t e r s [ 2 5 ] = a
: va r_dump () p r i n t _ r ( ) . p r i n t _ r () . $ c u s t o m e r s : p r i n t _ r ( $ c u s t o m e r s ) ;
:
A r r a y (
[1] => [2] => [3] =>
) , .
var_dump ( ) : va r_dump($cus tomer s ) ;
:
a r r a y ( 3 ) { [ 1 ] = > s t r i n g ( 9 ) " " [2]=> s t r i n g ( 9 ) " " [3]=> s t r i n g ( 1 0 ) " "
}
(, ). , , ,
var_dump () : a r r a y ( 2 ) {
["name"]=> s t r i n g ( 9 ) " " ["age"]=> i n t ( 1 2 )
>
i n t . a g e 12.
6. 91
,
. Web p r i n t _ r () var_dump () HTML, .. . ; HTML-, : echo ""; var_dump($customers); echo "";
, .
, . ,
$ c a p i t a l s , : $capitals[''] = " ";
" ",
(Austin) ( ). $ c a p i t a l s : $ c a p i t a l s [ ' R I ' ] = "";
, .
, , :
$customers[1] = $customers[2] = $customers[3] =
$customers[] = " "; $customers : $customers[1] $customers[2] = $customers[3] = $customers[4] =
, :
$customerCopy = $customers;
. , ,
$colors = array("", "", "", "", "")); . ,
. : $colors[3] = "";
$ c o l o r s [3] , . $ c o l o r s , . , -
:
unset($colors[ 3 ] ) ;
92 II.
$co lo r s : $colors[0] = $ c o l o r s [ l ] = $colors[2] = $colors [4] =
, 3 .
, ,
.
, ,
. ,
u n s e t ( $ c o l o r s ) ;
CofitfiufioeKa ,
, .
. . ,
.
. ,
,
s o r t (): sort($_);
. , ,
. , $ s t r e e t s : $streets[0] = ". "; $streets[l] = ". "; $streets[2] = "7- ";
sort($streets); :
$streets[0] = "7- "; $streets[lj = ". "; $streets[2] = ". ";
s o r t () , , .
a s o r t ( ) : a s o r t ( $ c a p i t a l s ) ;
,
. $ c a p i t a l s , : $cap i t a l s [ ' ' ] = ""; $cap i t a l s [ 'TX ' ] = ""; $cap i t a l s [ 'OR ' ] = "";
6. 93
a s o r t ( $ c a p i t a l s ) ; :
$ c a p i t a l s [ ' T X ' ] = "" $cap i t a l s [ ' ' ] = "" $cap i t a l s [ 'OR ' ] = ""
, 1-. .
, . ,
$cap i t a l s [1 ] = ""; $cap i t a l s [2 ] = "",-$cap i t a l s [3 ] = "";
a s o r t () $cap i t a l s [2 ] = "" $ c a p i t a l s [ l ] = "" $cap i t a l s [3 ] = ""
.
. . 6.1 .
6.1.
, 1
,
,
,
, _
( 8) ( ) ;, "" . ,
d a y l , day5 , d a y l l , day2 : d a y l , d a y 2 , d a y s , d a y l l . s o r t () : d a y l , d a y l l , day2, day5
, . ; 1 .
, , , :
$CAcapital = $capitals['']; echo $CAcapital,-
s o r t ($_)
a s o r t ($_)
r s o r t ($_)
a r sor t ($_)
ksor t ($_) k r s o r t ($_) u s o r t ($_, _) n a t s o r t ($_)
94 II.
,
: $CAcapital = $ c a p i t a l s [ ' C A x ' ] ;
$ c a p i t a l s , : Notice: Undefined index: CAx in d: \testarray.php on l i n e 9 (: : d:\testarray.php 9)
. $CAcapital , echo . @: @$CAcapital = $ c a p i t a l s [ ' ' ] ;
echo echo
echo $ c a p i t a l s [ ' ' ] ; :
,
: echo " { $ c a p i t a l s [ ' ' ] } " ;
:
l i s t
. l i s t () . , ,
$shoeInfo = array("", "", 22 .00) ; , p r i n t _ r () :
p r in t_r ($shoeInfo) ; :
Array
[0] => [1] => [ 2 ] = > 22
) l i s t :
l i s t ( $ f i r s t , $second) = $shoeInfo; echo $second, " " , $ f i r s t ;
, $f i r s t $second, $shoeInf , : $ f i r s t = $shoelnfo[0] ; $second = $shoe!n fo [ l ] ;
6. 95
, $shoeInfo, , l i s t () . echo
, .
key (). , $ s h o e I n f o [ ' s t y l e ' ] = "";
echo $shoeInf : $value = $ s h o e I n f o [ ' s t y l e ' ] ; $key = key($shoeInf) ; echo "$key: $value";
:
$value "", - key () $ . , ,
, . ; , .
/ / .
, ,
6. , , .. , (traversing). .
S . ,
*/ f oreach. ; .
.
, .
, .
.
S c u r r e n t (%_). , .
/ next ($_). . S p r e v i o u s ($_). . S end (%_). . S r e s e t ($_). .
,
:
96 II.
$value = current($capitals); echo "$value"; $value = next($capitals); echo "$value"; $value = next($capitals); echo "$value";
,
.
, ,
r e s e t () : r e s e t ( $ c a p i t a l s ) ;
echo ( 50 ). .
.
. , ,
, next, . , ,
, f reach, . .
foreach foreach
. : foreach{$_ as $ => ^) {
; }
foreach . S $_. . f $. ,
. , ..
$. J $. ,
.
, fo reach : $s ta te_popula t ion = a r r ay ("CA" => 34501130,
"WY" => 494423, "OR" => 3472867);
k s o r t ( $ s t a t e _ p o p u l a t i o n ) ; fo reach($s ta te_popula t ion as $ s t a t e => $population)
$population = number_format($population); echo " $ s t a t e : $populat ion.";
> Web- :
: 34,501,130 OR: 3,472,867 WY: 494,423
6. 97
: fo reach($s ta te_popula t ion as $population)
( ) , .
foreach , .. r e s e t ( ) .
Onftec/ //
var_dump () p r i n t _ r () (. " "). .
: count () s i z eo f ( ) . : $n = count($_ ); $n = sizeof($_);
$ .
7Lfie6 [1] => [2] =>
) $ s t r i n g l . , .
$resString = implode("", $);
98 II.
$ r e s S t r i n g , $, . : $ar rayln = array("", "", ""); $str ingOut = implode(" ;" , $ a r r a y l n ) ; echo $s t r ingOut ;
implode () $s t r ingOut . explode (), implode () , . -
, .
:
;; ,
implode ( ) . : $str ingOut = implode("; ", $a r r ay In ) ;
, :
; ;
^ [ ( HaoJoftotn)
,
.
. ,
( 1) . , f o reach .
.
e x t r a c t () compact () . e x t r a c t () , . ,
.
, e x t r a c t () : $testarray = array("pink" => "", "red" => ""); extract($testarray); echo " $red.\n"; echo " $pink.";
:
.
.
compact () . .
:
$colorl = ""; $color2 = ""; $ = ""; $ = "";
6. 99
$arrayln = a r rayC 'a" , "b" ) ; $arrayOut = compact ( "co lo r l " , "color2" , $ a r ray In) ,-
:
$ar rayOut[co lor l ] = $arrayOut[color2] = $arrayOut[a] = $arrayOut[b] =
, .
, compact () - .
S . $ c o l o r l $color2.
S , . $ a r r a y l n $ $. compact () $arrayOut.
. ,
, .
,
, compact ( ) .
. , ,
, .
, .
,
. , , : $subArray = array_sl ice ($, nl, 2) ;
nl , $ (0 , 1 ), 2 . :
$ t e s t a r r a y = array("", "", "", ""); $subArray = a r r a y _ s l i c e ( $ t e s t a r r a y , 1, 2 ) ;
$ subArray [0] => [1] =>
$ t e s t a r r a y 1 .
0, 1, " ", " ".
,
:
$bigArray = array_merge($2, %2, ...);
100 II.
: $arrayl = array("", ""); $array2 = array("", ""); $bigArray = array_merge($arrayl, $array2);
$bigArray: $bigArray[0] = $bigArray[1] = $bigArray[2] = $bigArray[3] =
,
. , ,
. :
$a r ray l = a r r a y ( " c o l o r 1 " => "", "color2" => ""); $array2 = a r r a y ( " c o l o r l " => "", "color3" => ""); $bigArray = array_merge($arrayl , $a r ray2) ;
:
$bigArray[color l ] = $bigArray[color2] = $bigArray[color3] =
" $ 2 ^ . , - a r r a y _ m e r g e _ r e c u r s i v e ( ) , array_merge () . W^iL/ , -^"^ .
" " .
, .
.
$diffArray = array_diff(%1, $2, ...); a r r a y _ d i f f () $di f f Array
$ a r r a y l , $2. . : $arrayl = '" => "", "" => "", "" => ""); $array2 = array("", "", ""); $diffArray = array_diff($arrayl, $array2);
: $diffArray[a] = ;
"" $ a r r a y l , $2, $dif f Array "".
a r r a y _ d i f f () . ,
$diffArray = a r r ay_d i f f ($a r r ay2 , $ a r r a y l ) ; $dif f Array $diffArray[0] = ;
, $2, $ a r r a y l , " ". $dif f Array.
6. 101
, , ,
:
$ d i f f A r r a y = a r r a y _ d i f f _ a s s o c ( $ a r r a y l , $ a r r a y 2 ) ; a r r a y _ d i f f _ a s s o c () ,
, :
$ d i f f A r r a y [ a ] = $ d i f f A r r a y [ b ] = $ d i f f A r r a y [ ] =
$ a r r a y l $ 2 , .
, 1 $s imArray = a r r a y _ i n t e r s e c t ( % 1 , $2, ...);
:
$s imArray = a r r a y _ i n t e r s e c t ( $ a r r a y l , $ a r r a y 2 ) ; $ s i m A r r a y :
$ s imArray [b ] = $ s i m A r r a y [ c ] =
a r r a y _ i n t e r s e c t , . , ,
a r r a y _ i n t e r s e c t _ a s s o c ( ) , : $s imArray = a r r a y _ i n t e r s e c t _ a s s o c ( $ a r r a y l , $ a r r a y 2 ) ;
$ s i m A r r a y $ a r r a y l $ 2 , . , , -
.
Dfiifiue / .
S . S . S .
$sum = a r r a y _ s u m ( $ a r r a y ) ; :
$ar rayAdd = a r r a y ( 3 , 3 , 3 ) ; $sum = a r r a y _ s u m ( $ a r r a y A d d ) ; echo $sum;
9. . , mate 5,
( "") 0.
102 II.
. ,
, .
,
$names = array("", "", "", ""); $names2 = array_unique($names);
$names2 : $names2[0] => $names2[l] => $names2[3] =>
, .
( , ) . , , :
$ t e s t a r r a y [ ' r o s e ' ] = red $ t e s t a r r a y [ ' i r i s ' ] = purple
$arrayFl ipped = a r r a y _ f l i p ( $ t e s t a r r a y ) ; :
$testarray['red'] = rose . . $testarray['purple'] = iris
-.
. ,
, :
S (onion) 0,50; S (apple) 2,50; (orange) 2,00; S (bacon) 3,50; S (potato) 1,00; S (ham) 5,00.
:
$foodPrices['onion'] = 0.50; $foodPrices['apple'] = 2.50; $foodPrices['orange'3 = 2.00; $foodPrices['bacon'] =3.50; ' S f o o d P r i c e s [ ' p o t a t o ' ] = 1.00; $foodPrices[ 'ham'] = 5.00,-
$foodPrices. , 3000 . 3000 , , (onion) (ham).
6. 103
: (vegetable), (fiuit) (meat). , , .
, :
$ f o o d P r i c e s [ ' v e g e t a b l e ' ] [ ' o n i o n ' ] = 0.50; $foodPrices [ ' v e g e t a b l e ' ] [ ' p o t a t o ' ] = 1.00; $ f o o d P r i c e s [ ' f r u i t ' ] [ ' a p p l e ' ] = 2 .50; $ f o o d P r i c e s [ ' f r u i t ' ] [ ' o r a n g e ' ] = 2 .00; $ f o o d P r i c e s [ ' m e a t ' ] [ ' b a c o n ' ] = 3.50; $ foodPr i ces [ 'mea t ' ] [ ' ham ' ] = 5.00;
(multidimensional), . . 6.1 $f oodPr ices . ;;, $f oodPrices -. >- vegetable , f r u i t meat . , meat -: bacon-3 . 5 0 ham-5 .00.
$foodPrices
vegetab le
f r u i t
meat
onion po ta to orange apple bacon ham
0,50 1,00 2,00 2,50 3,50 5,00
. 6. /. $foodPrices -
$f oodPr ices . -, -, .
.
, .
, :
$ f o o d P r i c e s [ ' v e g e t a b l e ' ] [ ' p o t a t o ' ] = 1.00; $ f o o d P r i c e s [ ' f r u i t ' ] [ ' a p p l e ' ] = 2 .50;
:
t r a n s p o r t a t i o n [ ' c a r ' ] [ ] = ""; t r a n s p o r t a t i o n [ ' c a r ' ] [ ] = "";
, 0: t r a n s p o r t a t i o n [ c a r ] [ 0 ] = ; t r a n s p o r t a t i o n [ ] [ 1 ] = ;
a r ray : $foodPrices = array(
"vegetable" > array("potato" =>1.00, "onion" => .50), "fruit" => array("apple" => 2.50, "orange" -> 2.00));
104 II.
, $foodPrices vege tab le f r u i t array. , . :
$ foodPr ices [vege tab le ] [po ta to] = 1.00 $foodPrices[vegetable] [onion] = .50 $ foodPr i ce s [ f ru i t ] [ app le ] = 2.50 $ foodPr ices [ f ru i t ] [o range] = 2.00
, , p r i n t _ r () var_dump () . : ar ray(2) {
["vegetable"] => a r ray (2 ){
["potato"] => f l o a t ( l ) ["onion"] => f l o a t ( 0 . 5 )
} [ " f ru i t " ] => a r ray (2 ){
["apple"] => f l o a t ( 2 . 5 ) ["orange"] => f loa t (2 )
} }
, .
v e g e t a b l e f l o a t p o t a t o onion 1 0 . 5 . f r u i t .
, . ,
:
$hamPrice = $foodPrices['meat']['ham']; echo:
echo $ f o o d P r i c e s [ ' m e a t ' ] [ ' h a m ' ] ; ,
. $ ( ) : echo " \ $ { $ f o o d P r i c e s [ ' m e a t ' ] [ ' h a m ' ] } " ;
, $ \ . , $ , . :
$5
( ),
6. 105
.
, .
, . , ,
implode () , . , . ,
, : $ resS t r ing = implode(" :" , $ f o o d P r i c e s [ ' v e g e t a b l e ' ] ) ;
$f oodPr ices [ ' v e g e t a b l e ' ] , - ": ".
implode () , : 1: 0.5
1 0.5 $ f oodPr ices [ ' v e g e t a b l e ' ] p o t a t o onion , , implode () .
f oreach ( . ). , $f oodPrices, , f oreach: . ( (nesting).)
.
foreach ($foodPrices as $category ) {
foreach ($category as $food =>$price ) {
$f_price =sp r in t f ( "%01 .2 f " ,$p r i ce ) ; echo "$food: \$$f_price \ n " ;
} }
: onion: $0.50 p o t a t o : $1.00 apple : $2.50 orange: $2.00 bacon: $3.50 ham: $5.00
, f oreach.
1. - $ f oodPrices, $ca tegory ( ).
2. $category: $f ood, $pr ice .
3. $ p r i c e . 4. . 5. $category . 6. $ p r i c e ,
.
106 II.
7. $ c a t e g o r y f oreach.
8. $foodPr ices f oreach, $category.
9. 1-8 , $category. , f oreach.
, f o reach v e g e t a b l e -, $category. , f oreach $ca tegory . f oreach, - $f oodPr ices , .. .
, PHJ* ,
. .
, (, ) $_SERVER. , $_SERVER [' PHP_SELF' ] .
.
4.1.0 (superglobal) (autoglobals), , . ( 8.) , $HTTP_SERVER_VARS, , ( 8). ,
(_). , 4.1.0 .
php. i n i , 5, .
,
. php. i n i : r eg i s t e r_ long_a r r ays = On
On. , Of f.
On, , Off. , , , $HTTP_GET_VARS, php. i n i , . Off, , .
6. 107
. 6.2.
. , , ,
10.
6.2.
$GLOBALS
$_POST
$HTTP_POST_VARS $_GET
$HTTP_GET_VARS $_COOKIE $HTTP_COOKIE_VARS $_SESSION $HTTP_SESSION_VARS
$_REQUEST
$_FILES $HTTP_FILES_VARS $_SERVER
$HTTP_SERVER_VARS $_ENV
$HTTP_ENV_VARS
. ,
$ t e s t v a r 1, : $ G L O B A L S [ ' t e s t v a r ' ] , P O S , V method = "post" , $_POST , i URL-, , ;. GET: method = "get" , $_GET cookie
, $_COOKIE
, $ _ S E S S I O N , $ _ P O S T , $_GETH$SESSION ^
, $ _ F I L E S . Web- ,
, $_SERVER : , , . ,
, $_ENV
$_SERVER $_ENV $_SERVER $_ENV
. , ,
foreach($_SERVER as $key => $value) {
echo " = $key, = $value\n" ; }
:
Kni04=DOCTJMENT_ROOT, =:/program f i l e s / a p a c h e group/apache/htdocs
Knio4=PHP_SELF, 3Ha4eHHe=/test .php
108 II.
DOCUMENT_ROOT , Web- Apache Web-. PHP_SELF , .
$_ENV phpinf () 16: phpinf(16) ;
,
t r a c k - v a r s . 4.0.3, , .
. , t r a c k - v a r s , phpinf () . , , .
$argv $argc .
.
Web , CLI . , , , ,
. : php add.php 2 3
add. php , 2 3 , . , $argv. :
$argv[0] = add.php $argv[ l ] = 2 $argv[2] = 3
, $argv , :
$sum = $argv[l]+$argv[2]; echo $sum;
5 $argc.
$argv. $argc 1, . 3.
6. 109
III
PHP
&^ 9 ^, ^
3 / ! ,
,
. ,
. ,
-.
7
3 ... >
>
>
>
> i f >
>
HP- . , .
. ,
,
,
echo. ,
, .
// , /&
, , .
:
$ = " "; echo $; $ = " "; echo $a; ,
,
, :
$ = " "; echo $a; $ " "; echo $a;
,
, .. ,
. , :
i f ( ) {
$ = ;
?
echo $a; } or else if ( ) {
$a = ; echo $a,-
} ,
, .
, .
S . - .
, ,
, , -
. (conditional statement). .'HP i f switch.
S . . , .
, : ,
.
. , .[ , (loop). for, whi le do. . while.
:- , .. , .
. , >, . : , . , -
, .
, .
Jlfioeefitca (condition) , :
. , ,
.
. .
S ? , ? 15- ?
S ? , (>? ?
S ? ? ?
, .
. , ? , ? , ?
114 III. P\iP
, (. 7.1). 7.1.
== ? === , ? > , ? >= , ? < , ? = 1000000 , ,
(==). (=). . , i f ($weather = "") $weather "", , .
: TRUE () FALSE (). : $ == $
, , $ = 1 $ = 1, , $ = 1 $ = 2 .
(!). : $ ! = 21
7. 115
() . - . ,
26 . . ,
. . 7.2 , .
7.2. ,
$
?
( )
[ ]
[ ]
-
,
,
,
"
$
ger?m
g ( e r ) m
g [ e r ] m
g [ A e r ] m
g [ a - c ] m
,
,
germ, gem
germ
gem, grm
gym, gum
gam, gbm, gem
He
, 2
geam
Gem, grem
germ, g e l
gem, grem, germ
gdm, gxm, g a l
{}
( )
,
bldg[l- b ldg l l l , bldgl32 bldg, bldg555 3] +
in1,n2) ()
\ ( | I )
ge*m
ge{5}m
{ 2 , 5 }
g\*m
(|)
gm, geeem
g e e e e e m
aa, a a a , a a a a , 1 4 5 a a a a a g*m
germ, grm
geeeem, g e e e e e e m
1, a3
gem, g e r m
,
. ,
(TRUE). , .
7. 117
1 [--] . *
, , .
S [--]. , ( , ).
S .*. , .
: , . , 123 ?, .
2 (|)
.
S . . S ( | ). , .
(| ) .
, , .
3 [0 -9 ]{5} ( \ - [0 -9 ]{4 ) ) ?$
.
S [ 0 - 9 ] {5}. , . S \~. (\) , (-)
, .
S [ 0 - 9 ] {4}. , < [>. / ( ) ?.
, .
S $. ($) (.. ).
90001 90002-4323. , 9001 12-4321, .
4
. + @ . + \ . $ ,
.com. , . - .
S . +. , @.
S @. , , (\).
S .+. , .
118 III. PriP
S \ .. \ , (.) .
S com$. , com.
[email protected] j ohndoeSsomedomain.com.
, you@yourcompany. net , you@. com @you. com, .
e r e g ( ) . :
ereg("", ); , ,
, $name ( ) : ereg("[-- 1 - ]+$" , $name)
.
/ $ . , .
S . , ,
.
(, -). , . , ,
, ..
, .
S "". , ,
, .
. ,
, .
, .
. : 1 and |o r | 2 and |o r | and | o r | . . .
( ). S and ( ). , . S o r ( ). ,
.
*/ ( ). , , .
7. 119
. 7.3 , .
7.3. , ...
$ageBobby == 21 o r $ageBobby == 22 21 22 $ a g e s a i l y > 29 and $ s t a t e == "OR" 29 , $ a g e S a l l y > 29 o r $ s t a t e == "OR" 29 $ c i t y == "" x o r $ s t a t e == " O R " $ c i t y
$ s t a t e OR,
$ ! "" and $age < 13 , , 13
.
: , and, or. , :
$resCity == "" or $ resS ta te == "NV" and $name == "" (NV),
. ,
, . , ,
.
.
1. , (and). , $ r e s S t a t e $name NV . , -
, or. ( ) .
2. , (or). $resCi ty . , , .
,
(()) . , , -. , :
($resCity == "" or $ resS ta te == "NV") and $name == "" , .. -
, (or). *- ,
(NV). . 1. , (or).
, $ r e s C i t y $ r e s S t a t e NV. , .
( ) . 2. , (and). PHP
$name. , , .
120 III.
,
. .
.
, , ,
, , | | ( or) && ( and). . , $ < $ && $ > $ $ < $b and $ > $. | | or, && and.
onefiounofioe (conditional statement)
. .
S if. , , .
S switch. , , .
i f i f , ,
. i f :
i f ()
elseif {)
else
i f .
J if. , . , .
i f . e l s e i f e l s e , .
,
, e l s e i f , e l s e , i f.
^ "'
7. 121
/ e l s e i f . i f . - e l s e i f . .
, .
i f . e l s e i f e l s e , .
,
i f (.. e l s e i f else) , i f .
S e l s e . . i f e l s e . e.'.se , , ,
if, .
if. , . , tio- , :
i f ($score > 92) $grade = ""; $message = "";
lseif ($score 83) $grade = "B"; $message = "";
elseif ($score 74)
$grade = "C"; $message = "";
elseif ($score 62) $grade = "D"; $message = "";
lse
$grade = "F"; $message = " !";
echo $message. "\n"; echo " $grade\n";
i f . 1. $score 92.
, $grade , $message , , echo. $score
122 III.
92, $grade $message e l s e i f .
2. $score 92 83. 92 83, $ grade $message, , echo. $ sco re 83, $grade $message e l s e i f .
3. $score 83 74. 83 74, $grade $message , echo. $ sco re 74, $grade $message e l s e i f .
4. $score 74 62. 74 62, $grade $message echo. $score 62, $grade $message e l s e .
5. $grade F, a $message " !", echo.
, ,
, . ,
$grade, .. i f ($grade > 92) {
$grade = ""; }
:
if ($grade > 92) $grade = "";
.
, , .
if ,
, (!). : if ((" [-]*" , $ s t r ing ) ) {
$list[] = $string. "\n",-}
, $ s t r i n g . , $ s t r i n g , , , ,
. , :
7. 123
i f ( !(" [-]*" , $s t r ing) {
$l i s t [ ] = $str ing."\n"; }
- (!) $ l i s t , , , .. , $ s t r i n g .
if i f .
(nesting). , , , (Idaho, ID). , , , , , ;, . if: i f ($cus tS ta te == "ID") {
if ($EmailAdd = "") {
$contactMethod = " "; } else {
$contactMethod = " "; }
} else {
$contactMethod = " "; }
, . ,
. ,
$contactMethod ( ) , .
, .
switch i f .
, . ,
, .
?