1. PHP Manual
Stig Sæther Bakken
Alexander Aulbach
Egon Schmid
Jim Winstead
Lars Torben Wilson
Rasmus Lerdorf
Zeev Suraski
Andrei Zmievski
Jouni Ahto
Edited by
Stig Sæther Bakken
Egon Schmid
5. Table of Contents
Preface ...................................................................................................................................................................................45
About this Manual.........................................................................................................................................................45
I. Getting Started ..................................................................................................................................................................47
1. Introduction...............................................................................................................................................................47
What is PHP? ......................................................................................................................................................49
What can PHP do?...............................................................................................................................................49
A brief history of PHP.........................................................................................................................................49
2. Installation.................................................................................................................................................................51
Downloading the latest version ...........................................................................................................................53
Installation on UNIX systems .............................................................................................................................53
Apache Module Quick Reference..............................................................................................................53
Building .....................................................................................................................................................54
Unix/Linux installs..............................................................................................................................................54
Using Packages ..........................................................................................................................................54
Unix/HP-UX installs ...........................................................................................................................................54
Unix/Solaris installs ............................................................................................................................................55
Required software ......................................................................................................................................55
Using Packages ..........................................................................................................................................56
Unix/OpenBSD installs .......................................................................................................................................56
Using Ports.................................................................................................................................................56
Using Packages ..........................................................................................................................................56
Unix/Mac OS X installs ......................................................................................................................................56
Using Packages ..........................................................................................................................................56
Compiling for OS X server........................................................................................................................57
Complete list of configure options ......................................................................................................................58
Database.....................................................................................................................................................58
Ecommerce ................................................................................................................................................62
Graphics.....................................................................................................................................................63
Miscellaneous ............................................................................................................................................64
Networking ................................................................................................................................................70
PHP Behaviour ..........................................................................................................................................71
Server .........................................................................................................................................................71
Text and language ......................................................................................................................................73
XML ..........................................................................................................................................................73
Installation on Windows 9x/Me/NT/2000 systems .............................................................................................74
Windows InstallShield ...............................................................................................................................74
General Installation Steps ..........................................................................................................................74
Building from source .................................................................................................................................75
Preparations......................................................................................................................................76
Putting it all together........................................................................................................................76
Compiling.........................................................................................................................................77
Installation of Windows extensions ...........................................................................................................77
Servers-Apache ...................................................................................................................................................78
Details of installing PHP with Apache on Unix. .......................................................................................78
Details of installing PHP on Windows with Apache 1.3.x ........................................................................80
Servers-CGI/Commandline .................................................................................................................................80
Testing........................................................................................................................................................81
Benchmarking............................................................................................................................................81
Servers-fhttpd ......................................................................................................................................................81
Servers-Caudium .................................................................................................................................................81
Servers-IIS/PWS .................................................................................................................................................82
Windows and PWS/IIS 3 ...........................................................................................................................82
Windows and PWS 4 or newer ..................................................................................................................83
5
6. Windows NT/2000 and IIS 4 or newer ......................................................................................................83
Servers-Netscape and iPlanet ..............................................................................................................................84
Servers-OmniHTTPd Server ...............................................................................................................................85
OmniHTTPd 2.0b1 and up for Windows...................................................................................................85
Servers-Oreilly Website Pro ................................................................................................................................86
Oreilly Website Pro 2.5 and up for Windows ............................................................................................86
Servers-Xitami.....................................................................................................................................................86
Xitami for Windows ..................................................................................................................................86
Servers-Other web servers...................................................................................................................................87
Problems? ............................................................................................................................................................87
Read the FAQ.............................................................................................................................................87
Other problems ..........................................................................................................................................87
Bug reports.................................................................................................................................................87
3. Configuration ............................................................................................................................................................89
The configuration file ..........................................................................................................................................91
General Configuration Directives ..............................................................................................................91
Mail Configuration Directives ...................................................................................................................94
Safe Mode Configuration Directives .........................................................................................................95
Debugger Configuration Directives ...........................................................................................................95
Extension Loading Directives....................................................................................................................95
MySQL Configuration Directives..............................................................................................................95
mSQL Configuration Directives ................................................................................................................96
Postgres Configuration Directives .............................................................................................................96
SESAM Configuration Directives .............................................................................................................96
Sybase Configuration Directives ...............................................................................................................97
Sybase-CT Configuration Directives .........................................................................................................97
Informix Configuration Directives.............................................................................................................98
BC Math Configuration Directives ............................................................................................................98
Browser Capability Configuration Directives............................................................................................98
Unified ODBC Configuration Directives...................................................................................................98
4. Security ...................................................................................................................................................................101
Installed as CGI binary......................................................................................................................................103
Possible attacks........................................................................................................................................103
Case 1: only public files served ...............................................................................................................103
Case 2: using –enable-force-cgi-redirect .................................................................................................104
Case 3: setting doc_root or user_dir ........................................................................................................104
Case 4: PHP parser outside of web tree...................................................................................................104
Installed as an Apache module..........................................................................................................................105
Filesystem Security ...........................................................................................................................................105
Error Reporting .................................................................................................................................................106
User Submitted Data .........................................................................................................................................107
General considerations ......................................................................................................................................108
Keeping Current ................................................................................................................................................108
II. Language Reference ......................................................................................................................................................109
5. Basic syntax ............................................................................................................................................................109
Escaping from HTML .......................................................................................................................................111
Instruction separation ........................................................................................................................................111
Comments..........................................................................................................................................................111
6. Types .......................................................................................................................................................................113
Integers ..............................................................................................................................................................115
Floating point numbers......................................................................................................................................115
Strings................................................................................................................................................................115
String conversion .....................................................................................................................................117
Arrays ................................................................................................................................................................118
Single Dimension Arrays.........................................................................................................................118
6
7. Multi-Dimensional Arrays.......................................................................................................................118
Objects...............................................................................................................................................................120
Object Initialization .................................................................................................................................120
Type Juggling ....................................................................................................................................................120
Type Casting ............................................................................................................................................121
7. Variables..................................................................................................................................................................123
Basics ................................................................................................................................................................125
Predefined variables ..........................................................................................................................................125
Apache variables......................................................................................................................................126
Environment variables .............................................................................................................................127
PHP variables...........................................................................................................................................127
Variable scope ...................................................................................................................................................128
Variable variables ..............................................................................................................................................130
Variables from outside PHP ..............................................................................................................................131
HTML Forms (GET and POST)..............................................................................................................131
IMAGE SUBMIT variable names..................................................................................................131
HTTP Cookies .........................................................................................................................................131
Environment variables .............................................................................................................................132
Dots in incoming variable names.............................................................................................................132
Determining variable types......................................................................................................................132
8. Constants.................................................................................................................................................................133
9. Expressions .............................................................................................................................................................137
10. Operators...............................................................................................................................................................141
Arithmetic Operators.........................................................................................................................................143
Assignment Operators .......................................................................................................................................143
Bitwise Operators ..............................................................................................................................................143
Comparison Operators.......................................................................................................................................144
Error Control Operators ....................................................................................................................................144
Execution Operators ..........................................................................................................................................145
Incrementing/Decrementing Operators .............................................................................................................145
Logical Operators ..............................................................................................................................................146
Operator Precedence .........................................................................................................................................146
String Operators ................................................................................................................................................147
11. Control Structures .................................................................................................................................................149
if.......................................................................................................................................................................151
else ..................................................................................................................................................................151
elseif ..............................................................................................................................................................151
Alternative syntax for control structures ...........................................................................................................152
while ................................................................................................................................................................152
do..while .......................................................................................................................................................153
for ....................................................................................................................................................................154
foreach ............................................................................................................................................................155
break ................................................................................................................................................................157
continue ..........................................................................................................................................................157
switch ..............................................................................................................................................................158
require()............................................................................................................................................................159
include()............................................................................................................................................................160
require_once() ..................................................................................................................................................163
include_once() ..................................................................................................................................................165
12. Functions...............................................................................................................................................................167
User-defined functions ......................................................................................................................................169
Function arguments ...........................................................................................................................................169
Making arguments be passed by reference ..............................................................................................169
Default argument values ..........................................................................................................................170
Variable-length argument lists .................................................................................................................171
Returning values................................................................................................................................................171
7
8. old_function .................................................................................................................................................171
Variable functions..............................................................................................................................................171
13. Classes and Objects...............................................................................................................................................173
class ................................................................................................................................................................175
References inside the constructor......................................................................................................................177
14. References Explained............................................................................................................................................181
What References Are ........................................................................................................................................183
What References Do..........................................................................................................................................183
What References Are Not .................................................................................................................................183
Passing by Reference.........................................................................................................................................184
Returning References ........................................................................................................................................184
Unsetting References.........................................................................................................................................185
Spotting References...........................................................................................................................................185
global References..................................................................................................................................185
$this.......................................................................................................................................................185
III. Features.........................................................................................................................................................................187
15. Error Handling ......................................................................................................................................................187
16. Creating and manipulating images........................................................................................................................193
17. HTTP authentication with PHP ............................................................................................................................197
18. Cookies .................................................................................................................................................................201
19. Handling file uploads ............................................................................................................................................205
POST method uploads.......................................................................................................................................207
Common Pitfalls................................................................................................................................................208
Uploading multiple files ....................................................................................................................................209
PUT method support .........................................................................................................................................209
20. Using remote files .................................................................................................................................................211
21. Connection handling .............................................................................................................................................215
22. Persistent Database Connections ..........................................................................................................................219
IV. Function Reference ......................................................................................................................................................223
I. Apache-specific Functions.......................................................................................................................................223
apache_lookup_uri ............................................................................................................................................225
apache_note .......................................................................................................................................................225
getallheaders......................................................................................................................................................225
virtual ................................................................................................................................................................226
ascii2ebcdic .......................................................................................................................................................226
ebcdic2ascii .......................................................................................................................................................227
II. Array Functions......................................................................................................................................................229
array...................................................................................................................................................................231
array_count_values............................................................................................................................................232
array_diff ...........................................................................................................................................................232
array_flip ...........................................................................................................................................................232
array_intersect ...................................................................................................................................................233
array_keys .........................................................................................................................................................233
array_merge.......................................................................................................................................................234
array_merge_recursive ......................................................................................................................................234
array_multisort ..................................................................................................................................................235
array_pad ...........................................................................................................................................................236
array_pop...........................................................................................................................................................236
array_push .........................................................................................................................................................237
array_rand..........................................................................................................................................................237
array_reverse .....................................................................................................................................................238
array_shift..........................................................................................................................................................238
array_slice .........................................................................................................................................................239
array_splice .......................................................................................................................................................239
array_sum ..........................................................................................................................................................240
8