Manual do PHP

Stig Sæther Bakken
Alexander Aulbach
Egon Schmid
Jim Winstead
Lars Torben Wilson
Rasmus Lerdorf
Andrei Zmievski
Jouni Ahto

Editado por

Stig Sæther Bakken
Egon Schmid
Renato Arruda
André Bacci
Anderson Fortaleza
João Prado Maia
Claudio Pereira
Lucas Rocha
Alessander Pery Lopes Thomaz

17-07-2002

Copyright

Este manual é © Copyright 1997, 1998, 1999, 2000, 2001, 2002 pelo PHP Documentation Group. Os membros deste grupo são listados na primeira página deste manual.

Este manual pode ser redistribuído sob os termos da Licença Pública Geral GNU conforme publicada pela Free Software Foundation; a versão 2 da Licença ou (a seu critério) qualquer versão posterior.

A seção 'Extendendo PHP 4.0' deste manual tem copyright © 2000 por Zend Technologies, Ltd. Este material pode ser distribuido somente sob os termos e condições descritas na Open Publication License, v1.0 ou posterior (a versão mais recente é atualmente presente em http://www.opencontent.org/openpub/).


Índice
Prefácio
I. Começando
1. Introdução
2. A simple tutorial
3. Instalação
4. Configuração
5. Security
II. Referência da Linguagem
6. Sintaxe básica
7. Tipos
8. Variáveis
9. Constantes
10. Expressões
11. Operadores
12. Estruturas de Controle
13. Funções
14. Classes e Objetos
15. References Explained
III. Características
16. Error Handling
17. Creating and manipulating images
18. HTTP authentication with PHP
19. Cookies
20. Handling file uploads
21. Using remote files
22. Connection handling
23. Persistent Database Connections
24. Safe Mode
25. Usando o PHP na linha de comando
IV. Referência das Funções
I. Funções específicas para Apache
II. Funções para a manipulação de arrays
III. Aspell functions [deprecated]
IV. BCMath Funções de Matemática de Precisão Arbitrária
V. Bzip2 Compression Functions
VI. Calendar functions
VII. CCVS API Functions
VIII. COM support functions for Windows
IX. Class/Object Functions
X. ClibPDF functions
XI. Crack functions
XII. CURL, Client URL Library Functions
XIII. Cybercash payment functions
XIV. Crédit Mutuel CyberMUT functions
XV. Cyrus IMAP administration functions
XVI. Character type functions
XVII. Database (dbm-style) abstraction layer functions
XVIII. Date and Time functions
XIX. dBase functions
XX. DBM Functions
XXI. dbx functions
XXII. DB++ Functions
XXIII. Direct IO functions
XXIV. Funções de diretório
XXV. DOM XML functions
XXVI. .NET functions
XXVII. Error Handling and Logging Functions
XXVIII. FrontBase Functions
XXIX. filePro functions
XXX. Funções do Filesystem
XXXI. Forms Data Format functions
XXXII. FriBiDi functions
XXXIII. FTP functions
XXXIV. Function Handling functions
XXXV. Gettext
XXXVI. GMP functions
XXXVII. HTTP functions
XXXVIII. Hyperwave functions
XXXIX. Hyperwave API functions
XL. ICAP Functions [deprecated]
XLI. iconv functions
XLII. Image functions
XLIII. IMAP, POP3 and NNTP functions
XLIV. Informix functions
XLV. InterBase functions
XLVI. Ingres II functions
XLVII. IRC Gateway Functions
XLVIII. Java
XLIX. LDAP functions
L. Mail functions
LI. mailparse functions
LII. Funções Matemáticas
LIII. Multi-Byte String Functions
LIV. MCAL functions
LV. Mcrypt Encryption Functions
LVI. Mhash Functions
LVII. Mimetype Functions
LVIII. Microsoft SQL Server functions
LIX. Ming functions for Flash
LX. Miscellaneous functions
LXI. mnoGoSearch Functions
LXII. mSQL functions
LXIII. MySQL Functions
LXIV. Mohawk Software session handler functions
LXV. muscat functions
LXVI. Network Functions
LXVII. Ncurses terminal screen control functions
LXVIII. Lotus Notes functions
LXIX. Unified ODBC functions
LXX. Oracle 8 functions
LXXI. OpenSSL functions
LXXII. Oracle functions
LXXIII. Ovrimos SQL functions
LXXIV. Output Control Functions
LXXV. Object property and method call overloading
LXXVI. PDF functions
LXXVII. Verisign Payflow Pro functions
LXXVIII. PHP Options&Information
LXXIX. POSIX functions
LXXX. Funções PostgreSQL
LXXXI. Process Control Functions
LXXXII. Program Execution functions
LXXXIII. Printer functions
LXXXIV. Pspell Functions
LXXXV. GNU Readline
LXXXVI. GNU Recode functions
LXXXVII. Regular Expression Functions (Perl-Compatible)
LXXXVIII. qtdom functions
LXXXIX. Regular Expression Functions (POSIX Extended)
XC. Semaphore, Shared Memory and IPC Functions
XCI. SESAM database functions
XCII. Session handling functions
XCIII. Shared Memory Functions
XCIV. Shockwave Flash functions
XCV. SNMP functions
XCVI. Socket functions
XCVII. String functions
XCVIII. Sybase functions
XCIX. Tokenizer functions
C. Funções URL
CI. Variable Functions
CII. vpopmail functions
CIII. W32api functions
CIV. WDDX Functions
CV. XML parser functions
CVI. XMLRPC functions
CVII. XSLT functions
CVIII. YAZ functions
CIX. YP/NIS Functions
CX. Zip File Functions (Read Only Access)
CXI. Zlib Compression Functions
V. Extending PHP 4.0
26. Overview
27. Extension Possibilities
28. Source Layout
29. PHP's Automatic Build System
30. Creating Extensions
31. Using Extensions
32. Troubleshooting
33. Source Discussion
34. Accepting Arguments
35. Creating Variables
36. Duplicating Variable Contents: The Copy Constructor
37. Returning Values
38. Printing Information
39. Startup and Shutdown Functions
40. Calling User Functions
41. Initialization File Support
42. Where to Go from Here
43. Reference: Some Configuration Macros
44. API Macros
VI. FAQ: Perguntas Mais Freqüentes
45. General Information
46. Mailing lists
47. Obtaining PHP
48. Database issues
49. Installation
50. Build Problems
51. Using PHP
52. PHP and HTML
53. PHP and COM
54. PHP and other languages
55. Migrating from PHP 2 to PHP 3
56. Migrating from PHP 3 to PHP 4
57. Miscellaneous Questions
VII. Apêndices
A. History of PHP and related projects
B. Migrating from PHP 3 to PHP 4
C. Migrating from PHP/FI 2 to PHP 3
D. O debugador do PHP
E. Extending PHP
F. Lista de funções sinônimas (aliases)
G. Palavras reservadas do PHP
H. List of Resource Types
I. List of Parser Tokens
J. Sobre o manual
K. missing stuff

Prefácio

PHP, que significa "PHP: Hypertext Preprocessor", é uma linguagem de programação de ampla utilização interpretada que é especialmente interessante para desenvolvimento para a Web e pode ser misturada a ccódigo HTML. A sintaxe da linguagem lembra C C, Java e Perl, e é fácil de aprender. O objetivo principal da linguagem é deixar desenvolvedores escrever páginas que serão geradas dinâmicamente rapidamente, mas você pode fazer muito mais do que isso com PHP.

Esse manual consiste primáriamente de uma referência de funções, mas ele também contém uma referência da linguagem, explicações sobre as mais importantes características do PHP, e outras informações suplementares.

Você pode fazer o download desse manual em vários formatos em http://www.php.net/docs.php. Os downloads são atualizados automaticamente assim que o conteúdo for modificado. Mais informaçõeo sobre como esse manual é desenvolvido pode ser encontrado na página 'Sobre o manual' no apendice.


Capítulo 1. Introdução

O que é PHP?

PHP (um acrônimo recursivo para "PHP: Hypertext Preprocessor") é uma linguagem de script Open Source de uso geral, muito utilizada e especialmente guarnecida para o desenvolvimento de aplicações Web embútivel dentro do HTML.

Uma resposta simples, mas o que isso significa?

Exemplo 1-1. Um exemplo introdutório

<html>
    <head>
        <title>Exemplo</title>
    </head>
    <body>

        <?php
        echo "Olá, Eu sou um script PHP!";
        ?>

    </body>
</html>

Note como isso é diferente de scripts CGI escritos em outras linguagens como Perl ou C --- ao invés de escrever um programa com um monte de comandos para imprimir HTML, você escreve um arquivo HTML com algum código inserido para fazer alguma coisa (nesse caso, imprimir um pouco de texto). O código PHP é delimitado por tags iniciais e finais que lhe permitem pular pra dentro e pra fora do "modo PHP".

O que distingui o PHP de algo como Javascript no lado do cliente é que o código é executado no servidor. Se você tivesse um script similar ao acima em seu servidor, o cliente receberia os resultados da execução desse script, sem nenhum modo de determinar como é o código fonte. Você pode inclusive configurar seu servidor para processar todos os seus arquivos HTML como PHP, e então não haverá nenhum modo dos usuários descobrirem que se você usa essa linguagem ou não.

A melhor coisa em usar PHP está no fato de ele ser extremamente simples para um iniciante, mas oferece muitos recursos para o programador profissional. Não se preocupe em ler as longas listas de funções do PHP. Você pode pular essa parte (por enquanto) e começar a escrever scripts em poucas horas.

Apesar do desenvolvimento do PHP ser focado nos scripts do lado do servidor, você pode fazer muito mais com ele. Veja isso e leia mais na seção O que o PHP pode fazer?.


O que o PHP pode fazer?

Qualquer coisa. O PHP é focado para ser uma linguagem de script do lado do servidor, portanto, você pode fazer qualquer coisa que outro programa CGI pode fazer, como: coletar dados de formulários, gerar páginas com conteúdo dinâmico ou enviar e receber cookies. Mas o PHP pode fazer muito mais.

Esses são os maiores campos onde os scripts PHP podem se utilizados:

  • Script no lado do servidor (server-side). Este é o mais tradicional e principal campo de atuação do PHP. Você precisa de três coisas para seu trabalho. O interpretador do PHP (como CGI ou módulo), um servidor web e um browser. Basta rodar o servidor web conectado a um PHP instalado. Você pode acessar os resultados de seu programa PHP com um browser, visualizando a página PHP através do servidor web. Veja as instruções de instalação para maiores informações.

  • Script de linha de comando. Você pode fazer um script PHP funcionar sem um servidor web ou browser. A única coisa necessária é o interpretador. Esse tipo de uso é ideal para script executados usando o cron ou o Agendador de Tarefas (no Windows). Esses scripts podem ser usados também para rotinas de processamento de texto. Veja a seção Utilizando o PHP em linha de comando para maiores informações.

  • Escrevendo aplicações GUI no lado do cliente (client-side). O PHP não é (provavelmente) a melhor linguagem para produção de aplicações com interfaces em janelas, mas o PHP faz isso muito bem, e se você deseja usar alguns recursos avançados do PHP em aplicações no lado do cliente poderá utilizar o PHP-GTK para escrever esses programas. E programas escritos desta forma ainda serão independentes de plataforma. O PHP-GTK é uma extensão do PHP, não disponível na distribuição oficial. Se você está interessado no PHP-GTK, visite seu website

O PHP pode ser utilizado na maioria dos sistemas operacionais, incluindo Linux, várias variantes Unix (incluindo HP-UX, Solaris e OpenBSD), Microsoft Windows, Mac OS X, RISC OS, e provavelmente outros. O PHP também é suportado pela maioria dos servidores web atuais, incluindo Apache, Microsoft Internet Information Server, Personal Web Server, Netscape and iPlanet Servers, Oreilly Website Pro Server, Caudium, Xitami, OmniHTTPd, e muitos outros. O PHP pode ser configurado como módulo para a maioria dos servidores, e para os outros como um CGI comum.

Com o PHP, portanto, você tem a liberdade para escolher o sistema operacional e o servidor web. Do mesmo modo, você pode escolher entre utilizar programação estrutural ou programação orientada a objeto, ou ainda uma mistura deles. Mesmo não desenvolvendo nenhum recurso padrão de OOP (Object Oriented Programming, Programação Orientada a Objetos) na versão atual do PHP, muitas bibliotecas de código e grandes aplicações (incluindo a biblioteca PEAR) foram escritos somente utilizando OOP.

Com PHP você não está limitado a gerar somente HTML. As habilidades do PHP incluem geração de imagens, arquivos PDF e animações Flash (utilizando libswf ou Ming) criados dinamicamente, on the fly. Você pode facilmente criar qualquer padrão texto, como XHTML e outros arquivos XML. O PHP pode gerar esses padrões e os salvar no sistema de arquivos, em vez de imprimi-los, formando um cache dinâmico de suas informações no lado do servidor.

Talvez a mais forte e mais significativa característica do PHP é seu suporte a uma ampla variedade de banco de dados. Escrever uma página que consulte um banco de dados é incrivelmente simples. Os seguintes bancos de dados são atualmente suportados:

Adabas DIngresOracle (OCI7 and OCI8)
dBaseInterBaseOvrimos
EmpressFrontBasePostgreSQL
FilePro (read-only)mSQLSolid
HyperwaveDirect MS-SQLSybase
IBM DB2MySQLVelocis
InformixODBCUnix dbm

Também foi providenciado uma abstração de banco de dados DBX permitindo a você utilizar qualquer banco de dados transparentemente com sua extensão. Adicionalmente, o PHP suporta ODBC (Open Database Connection, ou Padrão Aberto de Conexão com Bancos de Dados), permitindo que você utilize qualquer outro banco de dados que suporte esse padrão mundial.

O PHP também tem suporte para comunicação com outros serviços utilizando protocolos como LDAP, IMAP, SNMP, NNTP, POP3, HTTP, COM (em Windows) e incontáveis outros. Você pode abrir sockets de rede e interagir diretamente com qualquer protocolo. O PHP também suporta o intercâmbio de dados complexos WDDX, utilizado em virtualmente todas as linguagens de programação para web. Falando de comunicação, o PHP implementa a instanciação de objetos Java e os utiliza transparentemente como objetos PHP. Você ainda pode usar sua extensão CORBA para acessar objetos remotos.

O PHP é extremamente útil em recursos de processamento de texto, do POSIX Estendido ou expressões regulares Perl até como interpretador para documentos XML. Para acessar e processar documentos XML, são suportados os padrões SAX e DOM. Você ainda pode usar nossa extensão XSLT para transformar documentos XML.

Utilizando o PHP no campo do e-commerce, você poderá usar as funções específicas para Cybescash, CyberMUT, Verysign Payflow Pro e CCVS, práticos sistemas de pagamento online.

Por último mas longe de terminar, temos também outras extensões interessantes: funções para o search engine mnoGoSearch, funções para Gateway IRC, vários utilitários de compressão (gzip, bz2), calendário e conversões de datas, tradução...

Como você pode ver, esta página não é suficiente para descrever todos os recursos e benefícios que o PHP pode oferecer. Leia nas seções sobre a Instalação do PHP, e veja a referência das funções para detalhes das extensões mencionadas aqui.


Capítulo 2. A simple tutorial

Here we would like to show the very basics of PHP in a short simple tutorial. This text only deals with dinamic webpage creation with PHP, though PHP is not only capable of creating webpages. See the section titled What can PHP do for more information.

PHP-enabled web pages are treated just like regular HTML pages and you can create and edit them the same way you normally create regular HTML pages.


What do I need?

In this tutorial we assume that your server has support for PHP activated and that all files ending in .php are handled by PHP. On most servers this is the default extension for PHP files, but ask your server administrator to be sure. If your server supports PHP then you don't need to do anything. Just create your .php files and put them in your web directory and the server will magically parse them for you. There is no need to compile anything nor do you need to install any extra tools. Think of these PHP-enabled files as simple HTML files with a whole new family of magical tags that let you do all sorts of things.


Your first PHP-enabled page

Create a file named hello.php under your webserver root directory with the following content:

Exemplo 2-1. Our first PHP script: hello.php

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo "Hello World<p>"; ?>
 </body>
</html>

Note that this is not like a CGI script. The file does not need to be executable or special in any way. Think of it as a normal HTML file which happens to have a set of special tags available to you that do a lot of interesting things.

This program is extremely simple and you really didn't need to use PHP to create a page like this. All it does is display: Hello World using the PHP echo() statement.

If you tried this example and it didn't output anything, or it prompted for download, or you see the whole file as text, chances are that the server you are on does not have PHP enabled. Ask your administrator to enable it for you using the Installation chapter of the manual. If you want to develop PHP scripts locally, see the downloads section. You can develop locally on any Operating system, be sure to install an appropriate web server too.

The point of the example is to show the special PHP tag format. In this example we used <?php to indicate the start of a PHP tag. Then we put the PHP statement and left PHP mode by adding the closing tag, ?>. You may jump in and out of PHP mode in an HTML file like this all you want.


Something Useful

Let's do something a bit more useful now. We are going to check what sort of browser the person viewing the page is using. In order to do that we check the user agent string that the browser sends as part of its HTTP request. This information is stored in a variable. Variables always start with a dollar-sign in PHP. The variable we are interested in right now is $_SERVER["HTTP_USER_AGENT"].

PHP Autoglobals Note: $_SERVER is a special reserved PHP variable that contains all web server information. It's known as an Autoglobal. See the related manual page on Autoglobals (also known as Superglobals) for more information. These special variables were introduced in PHP 4.1.0. Before this time, we used the older $HTTP_*_VARS arrays instead, such as $HTTP_SERVER_VARS. Although deprecated, these older variables still exist.

To display this variable, we can simply do:

Exemplo 2-2. Printing a variable (Array element)

<?php echo $_SERVER["HTTP_USER_AGENT"]; ?>

There are many types of variables available in PHP. In the above example we printed an Array element. Arrays can be very useful.

$_SERVER is just one variable that's automatically made available to you by PHP. A list can be seen in the Reserved Variables section of the manual or you can get a complete list of them by creating a file that looks like this:

Exemplo 2-3. Show all predefined variables with phpinfo()

<?php phpinfo(); ?>

If you load up this file in your browser you will see a page full of information about PHP along with a list of all the variables available to you.

You can put multiple PHP statements inside a PHP tag and create little blocks of code that do more than just a single echo. For example, if we wanted to check for Internet Explorer we could do something like this:

Exemplo 2-4. Example using control structures and functions

<?php
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) {
	echo "You are using Internet Explorer<br />";
}
?>

Here we introduce a couple of new concepts. We have an if statement. If you are familiar with the basic syntax used by the C language this should look logical to you. If you don't know enough C or some other language where the syntax used above is used, you should probably pick up any introductory PHP book and read the first couple of chapters, or read the Language Reference part of the manual. You can find a list of PHP books at http://www.php.net/books.php.

The second concept we introduced was the strstr() function call. strstr() is a function built into PHP which searches a string for another string. In this case we are looking for "MSIE" inside $_SERVER["HTTP_USER_AGENT"]. If the string is found, the function returns TRUE and if it isn't, it returns FALSE. If it returns TRUE, the if statement evaluates to TRUE and the code within its {braces} is executed. Otherwise, it's not. Feel free to create similar examples, with if, else, and other functions such as strtoupper() and strlen(). Each related manual page contains examples too.

We can take this a step further and show how you can jump in and out of PHP mode even in the middle of a PHP block:

Exemplo 2-5. Mixing both HTML and PHP modes

<?php
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) {
?>
<h3>strstr must have returned true</h3>
<center><b>You are using Internet Explorer</b></center>
<?php
} else {
?>
<h3>strstr must have returned false</h3>
<center><b>You are not using Internet Explorer</b></center>
<?php
}
?>

Instead of using a PHP echo statement to output something, we jumped out of PHP mode and just sent straight HTML. The important and powerful point to note here is that the logical flow of the script remains intact. Only one of the HTML blocks will end up getting sent to the viewer depending on if strstr() returned TRUE or FALSE In other words, if the string MSIE was found or not.


Dealing with Forms

One of the most powerful features of PHP is the way it handles HTML forms. The basic concept that is important to understand is that any form element in a form will automatically be available to your PHP scripts. Please read the manual section on Variables from outside of PHP for more information and examples on using forms with PHP. Here's an example HTML form:

Exemplo 2-6. A simple HTML form

<form action="action.php" method="POST">
 Your name: <input type="text" name="name" />
 Your age: <input type="text" name="age" />
 <input type="submit">
</form>

There is nothing special about this form. It is a straight HTML form with no special tags of any kind. When the user fills in this form and hits the submit button, the action.php page is called. In this file you would have something like this:

Exemplo 2-7. Printing data from our form

Hi <?php echo $_POST["name"]; ?>.
You are <?php echo $_POST["age"]; ?> years old.

It should be obvious what this does. There is nothing more to it. The $_POST["name"] and $_POST["age"] variables are automatically set for you by PHP. Earlier we used the $_SERVER autoglobal, now above we just introduced the $_POST autoglobal which contains all POST data. Notice how the method of our form is POST. If we used the method GET then our form information would live in the $_GET autoglobal instead. You may also use the $_REQUEST autoglobal if you don't care the source of your request data. It contains a mix of GET, POST, COOKIE and FILE data. See also the import_request_variables() function.


Using old code with new versions of PHP

Now that PHP has grown to be a popular scripting language, there are more resources out there that have listings of code you can reuse in your own scripts. For the most part the developers of the PHP language have tried to be backwards compatible, so a script written for an older version should run (ideally) without changes in a newer version of PHP, in practice some changes will usually be needed.

Two of the most important recent changes that affect old code are:

  • The deprecation of the old $HTTP_*_VARS arrays (which need to be indicated as global when used inside a function or method). The following autoglobal arrays were introduced in PHP 4.1.0. They are: $_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_REQUEST, and $_SESSION. The older $HTTP_*_VARS arrays, such as $HTTP_POST_VARS, still exist and have since PHP 3.

  • External variables are no longer registered in the global scope by default. In other words, as of PHP 4.2.0 the PHP directive register_globals is off by default in php.ini. The preferred method of accessing these values is via the autoglobal arrays mentioned above. Older scripts, books, and tutorials may rely on this directive being on. If on, for example, one could use $id from the URL http://www.example.com/foo.php?id=42. Whether on or off, $_GET['id'] is available.

For more details on these changes, see the section on predefined variables and links therein.


What's next?

With what you know now you should be able to understand most of the manual and also the various example scripts available in the example archives. You can also find other examples on the php.net websites in the links section: http://www.php.net/links.php.


Capítulo 3. Instalação


General Installation Considerations

Before installing first, you need to know what do you want to use PHP for. There are three main fields you can use PHP, as described in the What can PHP do? section:

  • Server-side scripting

  • Command line scripting

  • Client-side GUI applications

For the first and most common form, you need three things: PHP itself, a web server and a web browser. You probably already have a web browser, and depending on your operating system setup, you may also have a web server (eg. Apache on Linux or IIS on Windows). You may also rent webspace at a company. This way, you don't need to set up anything on your own, only write your PHP scripts, upload it to the server you rent, and see the results in your browser. You can find a list of hosting companies at http://hosts.php.net/.

While setting up the server and PHP on your own, you have two choices for the method of connecting PHP to the server. For many servers PHP has a direct module interface (also called SAPI). These servers include Apache, Microsoft Internet Information Server, Netscape and iPlanet servers. Many other servers have support for ISAPI, the Microsoft module interface (OmniHTTPd for example). If PHP has no module support for your web server, you can always use it as a CGI processor. This means you set up your server to use the command line executable of PHP (php.exe on Windows) to process all PHP file requests on the server.

If you are also interested to use PHP for command line scripting (eg. write scripts autogenerating some images for you offline, or processing text files depending on some arguments you pass to them), you always need the command line executable. For more information, read the section about writing command line PHP applications. In this case, you need no server and no browser.

With PHP you can also write client side GUI applications using the PHP-GTK extension. This is a completely different approach than writing web pages, as you do not output any HTML, but manage windows and objects within them. For more information about PHP-GTK, please visit the site dedicated to this extension. PHP-GTK is not included in the official PHP distribution.

From now on, this section deals with setting up PHP for web servers on Unix and Windows with server module interfaces and CGI executables.

Downloading PHP, the source code, and binary distributions for Windows can be found at http://www.php.net/. We recommend you to choose a mirror nearest to you for downloading the distributions.


Unix/HP-UX installs

This section contains notes and hints specific to installing PHP on HP-UX systems.

Exemplo 3-1. Installation Instructions for HP-UX 10

From: paul_mckay@clearwater-it.co.uk
04-Jan-2001 09:49
(These tips are for PHP 4.0.4 and Apache v1.3.9) 

So you want to install PHP and Apache on a HP-UX 10.20 box? 

1. You need gzip, download a binary distribution from
http://hpux.connect.org.uk/ftp/hpux/Gnu/gzip-1.2.4a/gzip-1.2.4a-sd-10.20.depot.Z
uncompress the file and install using swinstall 

2. You need gcc, download a binary distribution from 
http://gatekeep.cs.utah.edu/ftp/hpux/Gnu/gcc-2.95.2/gcc-2.95.2-sd-10.20.depot.gz 
gunzip this file and install gcc using swinstall. 

3. You need the GNU binutils, you can download a binary distribution from 
http://hpux.connect.org.uk/ftp/hpux/Gnu/binutils-2.9.1/binutils-2.9.1-sd-10.20.depot.gz 
gunzip and install using swinstall. 

4. You now need bison, you can download a binary distribution from 
http://hpux.connect.org.uk/ftp/hpux/Gnu/bison-1.28/bison-1.28-sd-10.20.depot.gz 
install as above. 

5. You now need flex, you need to download the source from one of the
http://www.gnu.org mirrors. It is in the <filename>non-gnu</filename> directory of the ftp site. 
Download the file, gunzip, then tar -xvf it. Go into the newly created flex
directory and do a ./configure, then a make, and then a make install 

If you have errors here, it's probably because gcc etc. are not in your
PATH so add them to your PATH. 

Right, now into the hard stuff. 

6. Download the PHP and apache sources. 

7. gunzip and tar -xvf them. 

We need to hack a couple of files so that they can compile ok. 

8. Firstly the configure file needs to be hacked because it seems to lose
track of the fact that you are a hpux machine, there will be a
better way of doing this but a cheap and cheerful hack is to put 
    lt_target=hpux10.20 
on line 47286 of the configure script. 

9. Next, the Apache GuessOS file needs to be hacked. Under
apache_1.3.9/src/helpers change line 89 from 
    "echo "hp${HPUXMACH}-hpux${HPUXVER}"; exit 0" 
to: 
    "echo "hp${HPUXMACH}-hp-hpux${HPUXVER}"; exit 0" 
    
10. You cannot install PHP as a shared object under HP-UX so you must compile
it as a static, just follow the instructions at the Apache page. 

11. PHP and apache should have compiled OK, but Apache won't start. you need
to create a new user for Apache, eg www, or apache. You then change lines 252
and 253 of the conf/httpd.conf in Apache so that instead of 
    User nobody 
    Group nogroup 
you have something like 
    User www 
    Group sys 

This is because you can't run Apache as nobody under hp-ux. 
Apache and PHP should then work. 

Hope this helps somebody,
Paul Mckay.

Unix/Linux installs

This section contains notes and hints specific to installing PHP on Linux distributions.


Using Packages

Many Linux distributions have some sort of package installation system, such as RPM. This can assist in setting up a standard configuration, but if you need to have a different set of features (such as a secure server, or a different database driver), you may need to build PHP and/or your webserver. If you are unfamiliar with building and compiling your own software, it is worth checking to see whether somebody has already built a packaged version of PHP with the features you need.


Unix/Mac OS X installs

This section contains notes and hints specific to installing PHP on Mac OS X Server.


Using Packages

There are a few pre-packaged and pre-compiled versions of PHP for Mac OS X. This can help in setting up a standard configuration, but if you need to have a different set of features (such as a secure server, or a different database driver), you may need to build PHP and/or your web server yourself. If you are unfamiliar with building and compiling your own software, it's worth checking whether somebody has already built a packaged version of PHP with the features you need.


Compiling for OS X server

There are two slightly different versions of Mac OS X, client and server. The following is for OS X Server.

Exemplo 3-2. Mac OS X server install

1. Get the latest distributions of Apache and PHP
2. Untar them, and run the configure program on Apache like so.
    ./configure --exec-prefix=/usr \ 
    --localstatedir=/var \ 
    --mandir=/usr/share/man \ 
    --libexecdir=/System/Library/Apache/Modules \ 
    --iconsdir=/System/Library/Apache/Icons \ 
    --includedir=/System/Library/Frameworks/Apache.framework/Versions/1.3/Headers \ 
    --enable-shared=max \ 
    --enable-module=most \ 
    --target=apache 

4. You may also want to add this line: 
    setenv OPTIM=-O2 
    If you want the compiler to do some optimization. 
    
5. Next, go to the PHP 4 source directory and configure it. 
    ./configure --prefix=/usr \ 
    --sysconfdir=/etc \ 
    --localstatedir=/var \ 
    --mandir=/usr/share/man \ 
    --with-xml \ 
    --with-apache=/src/apache_1.3.12 

    If you have any other additions (MySQL, GD, etc.), be sure to add
    them here. For the --with-apache string, put in the path to your 
    apache source directory, for example "/src/apache_1.3.12". 
6. make
7. make install    
    This will add a directory to your Apache source directory under
    src/modules/php4.
    
8. Now, reconfigure Apache to build in PHP 4.
    ./configure --exec-prefix=/usr \ 
    --localstatedir=/var \ 
    --mandir=/usr/share/man \ 
    --libexecdir=/System/Library/Apache/Modules \ 
    --iconsdir=/System/Library/Apache/Icons \ 
    --includedir=/System/Library/Frameworks/Apache.framework/Versions/1.3/Headers \ 
    --enable-shared=max \ 
    --enable-module=most \ 
    --target=apache \ 
    --activate-module=src/modules/php4/libphp4.a 

    You may get a message telling you that libmodphp4.a is out of date.
    If so, go to the src/modules/php4 directory inside your apache
    source directory and run this command: 

    ranlib libmodphp4.a 

    Then go back to the root of the apache source directory and run the
    above configure command again. That'll bring the link table up to
    date. 

9. make

10. make install

11. copy and rename the php.ini-dist file to your "bin" directory from your
    PHP 4 source directory:
    cp php.ini-dist /usr/local/bin/php.ini 

    or (if your don't have a local directory) 

    cp php.ini-dist /usr/bin/php.ini

Other examples for Mac OS X client and Mac OS X server are available at Stepwise.


Compiling for MacOS X client

Those tips are graciously provided by Marc Liyanage.

The PHP module for the Apache web server included in Mac OS X. This version includes support for the MySQL and PostgreSQL databases.

NOTE: Be careful when you do this, you could screw up your Apache web server!

Do this to install:

  • 1. Open a terminal window

  • 2. Type "wget http://www.diax.ch/users/liyanage/software/macosx/libphp4.so.gz", wait for download to finish

  • 3. Type "gunzip libphp4.so.gz"

  • 4. Type "sudo apxs -i -a -n php4 libphp4.so"

Now type "sudo open -a TextEdit /etc/httpd/httpd.conf" TextEdit will open with the web server configuration file. Locate these two lines towards the end of the file: (Use the Find command)
#AddType application/x-httpd-php .php 
   #AddType application/x-httpd-php-source .phps
Remove the two hash marks (#), then save the file and quit TextEdit.

Finally, type "sudo apachectl graceful" to restart the web server.

PHP should now be up and running. You can test it by dropping a file into your "Sites" folder which is called "test.php". Into that file, write this line: "<?php phpinfo() ?>".

Now open up 127.0.0.1/~your_username/test.php in your web browser. You should see a status table with information about the PHP module.


Unix/OpenBSD installs

This section contains notes and hints specific to installing PHP on OpenBSD.


Using Ports

This is the recommended method of installing PHP on OpenBSD, as it will have the latest patches and security fixes applied to it by the maintainers. To use this method, ensure that you have a recent ports tree. Then simply find out which flavors you wish to install, and issue the make install command. Below is an example of how to do this.

Exemplo 3-3. OpenBSD Ports Install Example

$ cd /usr/ports/www/php4
$ make show VARNAME=FLAVORS
 (choose which flavors you want from the list)
$ env FLAVOR="imap gettext ldap mysql gd" make install
$ /usr/local/sbin/php4-enable

Using Packages

There are pre-compiled packages available for your release of OpenBSD. These integrate automatically with the version of Apache installed with the OS. However, since there are a large number of options (called flavors) available for this port, you may find it easier to compile it from source using the ports tree. Read the packages(7) manual page for more information in what packages are available.


Unix/Solaris installs

This section contains notes and hints specific to installing PHP on Solaris systems.


Required software

Solaris installs often lack C compilers and their related tools. The required software is as follows:

  • gcc (recommended, other C compilers may work)

  • make

  • flex

  • bison

  • m4

  • autoconf

  • automake

  • perl

  • gzip

  • tar

In addition, you will need to install (and possibly compile) any additional software specific to your configuration, such as Oracle or MySQL.


Using Packages

You can simplify the Solaris install process by using pkgadd to install most of your needed components.


Installation on UNIX systems

This section will guide you through the general configuration and installation of PHP on Unix systems. Be sure to investigate any sections specific to your platform or web server before you begin the process.

Prerequisite knowledge and software:

  • Basic UNIX skills (being able to operate "make" and a C compiler, if compiling)

  • An ANSI C compiler (if compiling)

  • flex (for compiling)

  • bison (for compiling)

  • A web server

  • Any module specific components (such as gd, pdf libs, etc.)

There are several ways to install PHP for the Unix platform, either with a compile and configure process, or through various pre-packaged methods. This documentation is mainly focused around the process of compiling and configuring PHP.

The initial PHP setup and configuration process is controlled by the use of the commandline options of the configure script. This page outlines the usage of the most common options, but there are many others to play with. Check out the Complete list of configure options for an exhaustive rundown. There are several ways to install PHP:


Apache Module Quick Reference

PHP can be compiled in a number of different ways, but one of the most popular is as an Apache module. The following is a quick installation overview.

Exemplo 3-4. Quick Installation Instructions for PHP 4 (Apache Module Version)

1.  gunzip apache_1.3.x.tar.gz
2.  tar xvf apache_1.3.x.tar
3.  gunzip php-x.x.x.tar.gz
4.  tar xvf php-x.x.x.tar
5.  cd apache_1.3.x
6.  ./configure --prefix=/www
7.  cd ../php-x.x.x
8.  ./configure --with-mysql --with-apache=../apache_1.3.x --enable-track-vars
9.  make
10. make install
11. cd ../apache_1.3.x
12. ./configure --activate-module=src/modules/php4/libphp4.a
13. make
14. make install
15. cd ../php-x.x.x
16. cp php.ini-dist /usr/local/lib/php.ini
17. Edit your httpd.conf or srm.conf file and add: 
      AddType application/x-httpd-php .php

18. Use your normal procedure for restarting the Apache server. (You must
    stop and restart the server, not just cause the server to reload by
    use a HUP or USR1 signal.)

Building

When PHP is configured, you are ready to build the CGI executable. The command make should take care of this. If it fails and you can't figure out why, see the Problems section.


Installation on Windows systems

This section applies to Windows 95/98/Me and Windows NT/2000/XP. Do not expect PHP to work on 16 bit platforms such as Windows 3.1. Sometimes we refer to the supported Windows platforms as Win32.

There are two main ways to install PHP for Windows: either manually or by using the InstallShield installer.

If you have Microsoft Visual Studio, you can also build PHP from the original source code.

Once you have PHP installed on your Windows system, you may also want to load various extensions for added functionality.


Windows InstallShield

The Windows PHP installer available from the downloads page at http://www.php.net/, this installs the CGI version of PHP and, for IIS, PWS, and Xitami, configures the web server as well. Also note, that while the InstallShield installer is an easy way to make PHP work, it is restricted in many aspects, as automatic setup of extensions for example is not supported.

Install your selected HTTP server on your system and make sure that it works.

Run the executable installer and follow the instructions provided by the installation wizard. Two types of installation are supported - standard, which provides sensible defaults for all the settings it can, and advanced, which asks questions as it goes along.

The installation wizard gathers enough information to set up the php.ini file and configure the web server to use PHP. For IIS and also PWS on NT Workstation, a list of all the nodes on the server with script map settings is displayed, and you can choose those nodes to which you wish to add the PHP script mappings.

Once the installation has completed the installer will inform you if you need to restart your system, restart the server, or just start using PHP.

Atenção

Be aware, that this setup of PHP is not secure. If you would like to have a secure PHP setup, you'd better go on the manual way, and set every option carefully. This automatically working setup gives you an instantly working PHP installation, but it is not meant to be used on online servers.


Manual Installation Steps

This install guide will help you manually install and configure PHP on your Windows webserver. You need to download the zip binary distribution from the downloads page at http://www.php.net/. The original version of this guide was compiled by Bob Silva, and can be found at http://www.umesd.k12.or.us/php/win32install.html.

This guide provides manual installation support for:

  • Personal Web Server 3 and 4 or newer

  • Internet Information Server 3 and 4 or newer

  • Apache 1.3.x

  • OmniHTTPd 2.0b1 and up

  • Oreilly Website Pro

  • Xitami

  • Netscape Enterprise Server, iPlanet

PHP 4 for Windows comes in two flavours - a CGI executable (php.exe), and several SAPI modules (for example: php4isapi.dll). The latter form is new to PHP 4, and provides significantly improved performance and some new functionality.

Atenção

The SAPI modules have been significantly improved in the 4.1 release, however, you may find that you encounter possible server errors or other server modules such as ASP failing, in older systems.

If you choose one of the SAPI modules and use Windows 95, be sure to download the DCOM update from the Microsoft DCOM pages. For the ISAPI module, an ISAPI 4.0 compliant Web server is required (tested on IIS 4.0, PWS 4.0 and IIS 5.0). IIS 3.0 is NOT supported. You should download and install the Windows NT 4.0 Option Pack with IIS 4.0 if you want native PHP support.

The following steps should be performed on all installations before the server specific instructions.

  • Extract the distribution file to a directory of your choice. c:\php\ is a good start. You probably do not want to use a path in which spaces are included (for example: c:\program files\php is not a good idea). Some web servers will crash if you do.

  • You need to ensure that the DLLs which PHP uses can be found. The precise DLLs involved depend on which web server you use and whether you want to run PHP as a CGI or as a server module. php4ts.dll is always used. If you are using a server module (e.g. ISAPI or Apache) then you will need the relevant DLL from the sapi folder. If you are using any PHP extension DLLs then you will need those as well. To make sure that the DLLs can be found, you can either copy them to the system directory (e.g. winnt/system32 or windows/system) or you can make sure that they live in the same directory as the main PHP executable or DLL your web server will use (e.g. php.exe, php4apache.dll).

    The PHP binary, the SAPI modules, and some extensions rely on external DLLs for execution. Make sure that these DLLs in the distribution exist in a directory that is in the Windows PATH. The best bet to do it is to copy the files below into your system directory, which is typically:

    c:\windows\system for Windows 9x/ME
    c:\winnt\system32 for Windows NT/2000
    c:\windows\system32 for Windows XP

    The files to copy are:

    php4ts.dll, if it already exists there, overwrite it
    The files in your distribution's 'dlls' directory. If you have them already installed on your system, overwrite them only if something doesn't work correctly (Before overwriting them, it is a good idea to make a backup of them, or move them to another folder - just in case something goes wrong).

    Download the latest version of the Microsoft Data Access Components (MDAC) for your platform, especially if you use Microsoft Windows 9x/NT4. MDAC is available at http://www.microsoft.com/data/.

  • Copy your chosen ini file (see below) to your '%WINDOWS%' directory on Windows 9x/Me or to your '%SYSTEMROOT%' directory under Windows NT/2000/XP and rename it to php.ini. Your '%WINDOWS%' or '%SYSTEMROOT%' directory is typically:

    c:\windows for Windows 9x/ME/XP
    c:\winnt or c:\winnt40 for NT/2000 servers

    There are two ini files distributed in the zip file, php.ini-dist and php.ini-optimized. We advise you to use php.ini-optimized, because we optimized the default settings in this file for performance, and security. The best is to study all the ini settings and set every element manually yourself. If you would like to achieve the best security, then this is the way for you, although PHP works fine with these default ini files.

  • Edit your new php.ini file:

    • You will need to change the 'extension_dir' setting to point to your php-install-dir, or where you have placed your php_*.dll files. ex: c:\php\extensions

    • If you are using OmniHTTPd, do not follow the next step. Set the 'doc_root' to point to your webservers document_root. For example: c:\apache\htdocs or c:\webroot

    • Choose which extensions you would like to load when PHP starts. See the section about Windows extensions, about how to set up one, and what is already built in. Note that on a new installation it is advisable to first get PHP working and tested without any extensions before enabling them in php.ini.

    • On PWS and IIS, you can set the browscap.ini to point to: c:\windows\system\inetsrv\browscap.ini on Windows 9x/Me, c:\winnt\system32\inetsrv\browscap.ini on NT/2000, and c:\windows\system32\inetsrv\browscap.ini on XP.

    • Note that the mibs directory supplied with the Windows distribution contains support files for SNMP. This directory should be moved to DRIVE:\usr\mibs (DRIVE being the drive where PHP is installed.)

    • If you're using NTFS on Windows NT, 2000 or XP, make sure that the user running the webserver has read permissions to your php.ini (e.g. make it readable by Everyone).

  • For PWS give execution permission to the webroot:

    • Start PWS Web Manager

    • Edit Properties of the "Home"-Directory

    • Select the "execute"-Checkbox


Building from source

Before getting started, it is worthwhile answering the question: "Why is building on Windows so hard?" Two reasons come to mind:

  1. Windows does not (yet) enjoy a large community of developers who are willing to freely share their source. As a direct result, the necessary investment in infrastructure required to support such development hasn't been made. By and large, what is available has been made possible by the porting of necessary utilities from Unix. Don't be surprised if some of this heritage shows through from time to time.

  2. Pretty much all of the instructions that follow are of the "set and forget" variety. So sit back and try follow the instructions below as faithfully as you can.


Preparations

Before you get started, you have a lot to download...

  • For starters, get the Cygwin toolkit from the closest cygwin mirror site. This will provide you most of the popular GNU utilities used by the build process.

  • Download the rest of the build tools you will need from the PHP site at http://www.php.net/extra/win32build.zip.

  • Get the source code for the DNS name resolver used by PHP at http://www.php.net/extra/bindlib_w32.zip. This is a replacement for the resolv.lib library included in win32build.zip.

  • If you don't already have an unzip utility, you will need one. A free version is available from InfoZip.

Finally, you are going to need the source to PHP 4 itself. You can get the latest development version using anonymous CVS. If you get a snapshot or a source tarball, you not only will have to untar and ungzip it, but you will have to convert the bare linefeeds to crlf's in the *.dsp and *.dsw files before Microsoft Visual C++ will have anything to do with them.

Nota: Place the Zend and TSRM directories inside the php4 directory in order for the projects to be found during the build process.


Putting it all together

  • Follow the instructions for installing the unzip utility of your choosing.

  • Execute setup.exe and follow the installation instructions. If you choose to install to a path other than c:\cygnus, let the build process know by setting the Cygwin environment variable. On Windows 95/98 setting an environment variable can be done by placing a line in your autoexec.bat. On Windows NT, go to My Computer => Control Panel => System and select the environment tab.

Atenção

Make a temporary directory for Cygwin to use, otherwise many commands (particularly bison) will fail. On Windows 95/98, mkdir C:\TMP. For Windows NT, mkdir %SystemDrive%\tmp.

  • Make a directory and unzip win32build.zip into it.

  • Launch Microsoft Visual C++, and from the menu select Tools => Options. In the dialog, select the directories tab. Sequentially change the dropdown to Executables, Includes, and Library files, and ensure that cygwin\bin, win32build\include, and win32build\lib are in each list, respectively. (To add an entry, select a blank line at the end of the list and begin typing). Typical entries will look like this:

    • c:\cygnus\bin

    • c:\php-win32build\include

    • c:\php-win32build\lib

    Press OK, and exit out of Visual C++.

  • Make another directory and unzip bindlib_w32.zip into it. Decide whether you want to have debug symbols available (bindlib - Win32 Debug) or not (bindlib - Win32 Release). Build the appropriate configuration:

    • For GUI users, launch VC++, and then select File => Open Workspace and select bindlib. Then select Build=>Set Active Configuration and select the desired configuration. Finally select Build=>Rebuild All.

    • For command line users, make sure that you either have the C++ environment variables registered, or have run vcvars.bat, and then execute one of the following:

      • msdev bindlib.dsp /MAKE "bindlib - Win32 Debug"

      • msdev bindlib.dsp /MAKE "bindlib - Win32 Release"

    • At this point, you should have a usable resolv.lib in either your Debug or Release subdirectories. Copy this file into your win32build\lib directory over the file by the same name found in there.


Compiling

The best way to get started is to build the standalone/CGI version.

  • For GUI users, launch VC++, and then select File => Open Workspace and select php4ts. Then select Build=>Set Active Configuration and select the desired configuration. Finally select Build=>Rebuild All.

  • For command line users, make sure that you either have the C++ environment variables registered, or have run vcvars.bat, and then execute one of the following:

    • msdev php4ts.dsp /MAKE "php4ts - Win32 Debug_TS"

    • msdev php4ts.dsp /MAKE "php4ts - Win32 Release_TS"

    • At this point, you should have a usable php.exe in either your Debug_TS or Release_TS subdirectories.

Repeat the above steps with php4isapi.dsp (which can be found in sapi\isapi) in order to build the code necessary for integrating PHP with Microsoft IIS.


Installation of Windows extensions

After installing PHP and a webserver on Windows, you will probably want to install some extensions for added functionality. The following table describes some of the extensions available. You can choose which extensions you would like to load when PHP starts by uncommenting the: 'extension=php_*.dll' lines in php.ini. You can also load a module dynamically in your script using dl().

The DLLs for PHP extensions are prefixed with 'php_' in PHP 4 (and 'php3_' in PHP 3). This prevents confusion between PHP extensions and their supporting libraries.

Nota: In PHP 4.0.6 BCMath, Calendar, COM, FTP, MySQL, ODBC, PCRE, Session, WDDX and XML support is built in. You don't need to load any additional extensions in order to use these functions. See your distributions README.txt or install.txt for a list of built in modules.

Nota: Some of these extensions need extra DLLs to work. Couple of them can be found in the distribution package, in the 'dlls' folder but some, for example Oracle (php_oci8.dll) require DLLs which are not bundled with the distribution package.

Copy the bundled DLLs from 'DLLs' folder to your Windows PATH, safe places are:

c:\windows\system for Windows 9x/Me
c:\winnt\system32 for Windows NT/2000
c:\windows\system32 for Windows XP

If you have them already installed on your system, overwrite them only if something doesn't work correctly (Before overwriting them, it is a good idea to make a backup of them, or move them to another folder - just in case something goes wrong).

Tabela 3-1. PHP Extensions

ExtensionDescriptionNotes
php_bz2.dllbzip2 compression functionsNone
php_calendar.dllCalendar conversion functionsBuilt in since PHP 4.0.3
php_cpdf.dllClibPDF functionsNone
php_crack.dllCrack functionsNone
php3_crypt.dllCrypt functionsunknown
php_ctype.dllctype family functionsNone
php_curl.dllCURL, Client URL library functionsRequires: libeay32.dll, ssleay32.dll (bundled)
php_cybercash.dllCybercash payment functionsNone
php_db.dllDBM functionsDeprecated. Use DBA instead (php_dba.dll)
php_dba.dllDBA: DataBase (dbm-style) Abstraction layer functionsNone
php_dbase.dlldBase functionsNone
php3_dbm.dllBerkeley DB2 libraryunknown
php_domxml.dllDOM XML functionsRequires: libxml2.dll (bundled)
php_dotnet.dll.NET functionsNone
php_exif.dllRead EXIF headers from JPEGNone
php_fbsql.dllFrontBase functionsNone
php_fdf.dllFDF: Forms Data Format functions.Requires: fdftk.dll (bundled)
php_filepro.dllfilePro functionsRead-only access
php_ftp.dllFTP functionsBuilt-in since PHP 4.0.3
php_gd.dllGD library image functionsNone
php_gettext.dllGettext functionsRequires: gnu_gettext.dll (bundled)
php_hyperwave.dllHyperWave functionsNone
php_iconv.dllICONV characterset conversionRequires: iconv-1.3.dll (bundled)
php_ifx.dllInformix functionsRequires: Informix libraries
php_iisfunc.dllIIS management functionsNone
php_imap.dllIMAP POP3 and NNTP functionsPHP 3: php3_imap4r1.dll
php_ingres.dllIngres II functionsRequires: Ingres II libraries
php_interbase.dllInterBase functionsRequires: gds32.dll (bundled)
php_java.dllJava extensionRequires: jvm.dll (bundled)
php_ldap.dllLDAP functionsRequires: libsasl.dll (bundled)
php_mhash.dllMhash FunctionsNone
php_ming.dllMing functions for FlashNone
php_msql.dllmSQL functionsRequires: msql.dll (bundled)
php3_msql1.dllmSQL 1 clientunknown
php3_msql2.dllmSQL 2 clientunknown
php_mssql.dllMSSQL functionsRequires: ntwdblib.dll (bundled)
php3_mysql.dllMySQL functionsBuilt-in in PHP 4
php3_nsmail.dllNetscape mail functionsunknown
php3_oci73.dllOracle functionsunknown
php_oci8.dllOracle 8 functionsRequires: Oracle 8 client libraries
php_openssl.dllOpenSSL functionsRequires: libeay32.dll (bundled)
php_oracle.dllOracle functionsRequires: Oracle 7 client libraries
php_pdf.dllPDF functionsNone
php_pgsql.dllPostgreSQL functionsNone
php_printer.dllPrinter functionsNone
php_xslt.dllXSLT functionsRequires: sablot.dll (bundled)
php_snmp.dllSNMP get and walk functionsNT only!
php_sybase_ct.dllSybase functionsRequires: Sybase client libraries
php_yaz.dllYAZ functionsNone
php_zlib.dllZLib compression functionsNone


Servers-CGI/Commandline

The default is to build PHP as a CGI program. This creates a commandline interpreter, which can be used for CGI processing, or for non-web-related PHP scripting. If you are running a web server PHP has module support for, you should generally go for that solution for performance reasons. However, the CGI version enables Apache users to run different PHP-enabled pages under different user-ids. Please make sure you read through the Security chapter if you are going to run PHP as a CGI.


Testing

If you have built PHP as a CGI program, you may test your build by typing make test. It is always a good idea to test your build. This way you may catch a problem with PHP on your platform early instead of having to struggle with it later.


Benchmarking

If you have built PHP 3 as a CGI program, you may benchmark your build by typing make bench. Note that if Safe Mode is on by default, the benchmark may not be able to finish if it takes longer then the 30 seconds allowed. This is because the set_time_limit() can not be used in safe mode. Use the max_execution_time configuration setting to control this time for your own scripts. make bench ignores the configuration file.

Nota: make bench is only available for PHP 3.


Using Variables

Some server supplied enviroment variables are not defined in the current CGI/1.1 specification. Only the following variables are defined there; everything else should be treated as 'vendor extensions': AUTH_TYPE, CONTENT_LENGTH, CONTENT_TYPE, GATEWAY_INTERFACE, PATH_INFO, PATH_TRANSLATED, QUERY_STRING, REMOTE_ADDR, REMOTE_HOST, REMOTE_IDENT, REMOTE_USER, REQUEST_METHOD, SCRIPT_NAME, SERVER_NAME, SERVER_PORT, SERVER_PROTOCOL and SERVER_SOFTWARE


Servers-Apache

This section contains notes and hints specific to Apache installs of PHP, both for Unix and Windows versions.


Details of installing PHP with Apache on Unix

You can select arguments to add to the configure on line 8 below from the Complete list of configure options. The version numbers have been omitted here, to ensure the instructions are not incorrect. You will need to replace the 'xxx' here with the correct values from your files.

Exemplo 3-5. Installation Instructions (Apache Shared Module Version) for PHP 4

1.  gunzip apache_xxx.tar.gz
2.  tar -xvf apache_xxx.tar
3.  gunzip php-xxx.tar.gz
4.  tar -xvf php-xxx.tar
5.  cd apache_xxx
6.  ./configure --prefix=/www --enable-module=so
7.  make
8.  make install
9.  cd ../php-xxx
10. ./configure --with-mysql --with-apxs=/www/bin/apxs
11. make
12. make install

  If you decide to change your configure options after installation
  you only need to repeat the last three steps. You only need to 
  restart apache for the new module to take effect. A recompile of
  Apache is not needed.

11. cp php.ini-dist /usr/local/lib/php.ini

  You can edit your .ini file to set PHP options.  If
  you prefer this file in another location, use
  --with-config-file-path=/path in step 8.

12. Edit your httpd.conf or srm.conf file and check that these lines are
    present and not commented out:
  
   AddType application/x-httpd-php .php

   LoadModule php4_module        libexec/libphp4.so
 
  You can choose any extension you wish here.  .php is simply the one
  we suggest. You can even include .html, and .php3 can be added for 
  backwards compatibility.
 
  The path on the right hand side of the LoadModule statement must point
  to the path of the PHP module on your system. The above statement is 
  correct for the steps shown above.


13. Use your normal procedure for starting the Apache server. (You must
    stop and restart the server, not just cause the server to reload by
    use a HUP or USR1 signal.)

Depending on your Apache install and Unix variant, there are many possible ways to stop and restart the server. Below are some typical lines used in restarting the server, for different apache/unix installations. You should replace /path/to/ with the path to these applications on your systems.

1. Several Linux and SysV variants:
/etc/rc.d/init.d/httpd restart

2. Using apachectl scripts:
/path/to/apachectl stop
/path/to/apachectl start

3. httpdctl and httpsdctl (Using OpenSSL), similar to apachectl:
/path/to/httpsdctl stop
/path/to/httpsdctl start

4. Using mod_ssl, or another SSL server, you may want to manually
stop and start:
/path/to/apachectl stop
/path/to/apachectl startssl

The locations of the apachectl and http(s)dctl binaries often vary. If your system has locate or whereis or which commands, these can assist you in finding your server control programs.

Different examples of compiling PHP for apache are as follows:

./configure --with-apxs --with-pgsql

This will create a libphp4.so shared library that is loaded into Apache using a LoadModule line in Apache's httpd.conf file. The PostgreSQL support is embedded into this libphp4.so library.

./configure --with-apxs --with-pgsql=shared

This will create a libphp4.so shared library for Apache, but it will also create a pgsql.so shared library that is loaded into PHP either by using the extension directive in php.ini file or by loading it explicitly in a script using the dl() function.

./configure --with-apache=/path/to/apache_source --with-pgsql

This will create a libmodphp4.a library, a mod_php4.c and some accompanying files and copy this into the src/modules/php4 directory in the Apache source tree. Then you compile Apache using --activate-module=src/modules/php4/libphp4.a and the Apache build system will create libphp4.a and link it statically into the httpd binary. The PostgreSQL support is included directly into this httpd binary, so the final result here is a single httpd binary that includes all of Apache and all of PHP.

./configure --with-apache=/path/to/apache_source --with-pgsql=shared

Same as before, except instead of including PostgreSQL support directly into the final httpd you will get a pgsql.so shared library that you can load into PHP from either the php.ini file or directly using dl().

When choosing to build PHP in different ways, you should consider the advantages and drawbacks of each method. Building as a shared object will mean that you can compile apache separately, and don't have to recompile everything as you add to, or change, PHP. Building PHP into apache (static method) means that PHP will load and run faster. For more information, see the Apache webpage on DSO support.

Nota: Apache's default http.conf currently ships with a section that looks like this:

User nobody
Group "#-1"

Unless you change that to "Group nogroup" or something like that ("Group daemon" is also very common) PHP will not be able to open files.

Nota: Make sure you specify the installed version of apxs when using --with-apxs=/path/to/apxs. You must NOT use the apxs version that is in the apache sources but the one that is actually installed on your system.


Installing PHP on Windows with Apache 1.3.x

There are two ways to set up PHP to work with Apache 1.3.x on Windows. One is to use the CGI binary (php.exe), the other is to use the Apache module DLL. In either case you need to stop the Apache server, and edit your srm.conf or httpd.conf to configure Apache to work with PHP.

It is worth noting here that now the SAPI module has been made more stable under windows, we recommend it's use above the CGI binary, since it is more transparent and secure.

Although there can be a few variations of configuring PHP under Apache, these are simple enough to be used by the newcomer. Please consult the Apache Docs for further configuration directives.

If you unziped the PHP package to c:\php\ as described in the Manual Installation Steps section, you need to insert these lines to your Apache configuration file to set up the CGI binary:

  • ScriptAlias /php/ "c:/php/"

  • AddType application/x-httpd-php .php .phtml

  • Action application/x-httpd-php "/php/php.exe"

Note that the second line in the list above can be found in the actual versions of httpd.conf, but it is commented out. Remember also to substitute the c:/php/ for your actual path to PHP.

Atenção

By using the CGI setup, your server is open to several possible attacks. Please read our CGI security section to learn how to defend yourself from attacks.

If you would like to use PHP as a module in Apache, be sure to move php4ts.dll to the windows/system (for Windows 9x/Me) or winnt/system32 (for Windows NT/2000/XP) directory, overwriting any older file. Then you should add the following two lines to you Apache conf file:

  • LoadModule php4_module c:/php/sapi/php4apache.dll

  • AddType application/x-httpd-php .php .phtml

After changing the configuration file, remember to restart the server, for example, NET STOP APACHE followed by NET START APACHE, if you run Apache as a Windows Service, or use your regular shortcuts.

Nota: You may find after using the windows installer for Apache that you need to define the AddModule directive for mod_php4.c in the configuration file (httpd.conf). This is done by adding AddModule mod_php4.c to the AddModule list, near the beginning of the configuration file. This is especially important if the ClearModuleList directive is defined. Failure to do this may mean PHP will not be registered as an Apache module.

There are 2 ways you can use the source code highlighting feature, however their ability to work depends on your installation. If you have configured Apache to use PHP as an ISAPI module, then by adding the following line to your configuration file you can use this feature: AddType application/x-httpd-php-source .phps

If you chose to configure Apache to use PHP as a CGI binary, you will need to use the show_source() function. To do this simply create a PHP script file and add this code: <?php show_source ("original_php_script.php"); ?>. Substitute original_php_script.php with the name of the file you wish to show the source of.

Nota: On Win-Apache all backslashes in a path statement such as "c:\directory\file.ext", must be converted to forward slashes, as "c:/directory/file.ext".


Servers-Caudium

PHP 4 can be built as a Pike module for the Caudium webserver. Note that this is not supported with PHP 3. Follow the simple instructions below to install PHP 4 for Caudium.

Exemplo 3-6. Caudium Installation Instructions

1.  Make sure you have Caudium installed prior to attempting to
    install PHP 4. For PHP 4 to work correctly, you will need Pike
    7.0.268 or newer. For the sake of this example we assume that
    Caudium is installed in /opt/caudium/server/.
2.  Change directory to php-x.y.z (where x.y.z is the version number).
3.  ./configure --with-caudium=/opt/caudium/server
4.  make
5.  make install
6.  Restart Caudium if it's currently running.
7.  Log into the graphical configuration interface and go to the
    virtual server where you want to add PHP 4 support.
8.  Click Add Module and locate and then add the PHP 4 Script Support module.
9.  If the documentation says that the 'PHP 4 interpreter isn't
    available', make sure that you restarted the server. If you did
    check /opt/caudium/logs/debug/default.1 for any errors related to
    <filename>PHP4.so</filename>. Also make sure that 
    <filename>caudium/server/lib/[pike-version]/PHP4.so</filename>
    is present.
10. Configure the PHP Script Support module if needed.

You can of course compile your Caudium module with support for the various extensions available in PHP 4. See the complete list of configure options for an exhaustive rundown.

Nota: When compiling PHP 4 with MySQL support you must make sure that the normal MySQL client code is used. Otherwise there might be conflicts if your Pike already has MySQL support. You do this by specifying a MySQL install directory the --with-mysql option.


Servers-fhttpd

To build PHP as an fhttpd module, answer "yes" to "Build as an fhttpd module?" (the --with-fhttpd=DIR option to configure) and specify the fhttpd source base directory. The default directory is /usr/local/src/fhttpd. If you are running fhttpd, building PHP as a module will give better performance, more control and remote execution capability.


Servers-IIS/PWS

This section contains notes and hints specific to IIS (Microsoft Internet Information Server). Installing PHP for PWS/IIS 3, PWS 4 or newer and IIS 4 or newer versions.


Windows and PWS/IIS 3

The recommended method for configuring these servers is to use the REG file included with the distribution (pws-php4cgi.reg). You may want to edit this file and make sure the extensions and PHP install directories match your configuration. Or you can follow the steps below to do it manually.

Atenção

These steps involve working directly with the Windows registry. One error here can leave your system in an unstable state. We highly recommend that you back up your registry first. The PHP Development team will not be held responsible if you damage your registry.

  • Run Regedit.

  • Navigate to: HKEY_LOCAL_MACHINE /System /CurrentControlSet /Services /W3Svc /Parameters /ScriptMap.

  • On the edit menu select: New->String Value.

  • Type in the extension you wish to use for your php scripts. For example .php

  • Double click on the new string value and enter the path to php.exe in the value data field. ex: c:\php\php.exe.

  • Repeat these steps for each extension you wish to associate with PHP scripts.

The following steps do not affect the web server installation and only apply if you want your php scripts to be executed when they are run from the command line (ex. run c:\myscripts\test.php) or by double clicking on them in a directory viewer window. You may wish to skip these steps as you might prefer the PHP files to load into a text editor when you double click on them.

  • Navigate to: HKEY_CLASSES_ROOT

  • On the edit menu select: New->Key.

  • Name the key to the extension you setup in the previous section. ex: .php

  • Highlight the new key and in the right side pane, double click the "default value" and enter phpfile.

  • Repeat the last step for each extension you set up in the previous section.

  • Now create another New->Key under HKEY_CLASSES_ROOT and name it phpfile.

  • Highlight the new key phpfile and in the right side pane, double click the "default value" and enter PHP Script.

  • Right click on the phpfile key and select New->Key, name it Shell.

  • Right click on the Shell key and select New->Key, name it open.

  • Right click on the open key and select New->Key, name it command.

  • Highlight the new key command and in the right side pane, double click the "default value" and enter the path to php.exe. ex: c:\php\php.exe -q %1. (don't forget the %1).

  • Exit Regedit.

  • If using PWS on Windows, reboot to reload the registry.

PWS and IIS 3 users now have a fully operational system. IIS 3 users can use a nifty tool from Steven Genusa to configure their script maps.


Windows and PWS 4 or newer

When installing PHP on Windows with PWS 4 or newer version, you have two options. One to set up the PHP CGI binary, the other is to use the ISAPI module DLL.

If you choose the CGI binary, do the following:

  • Edit the enclosed pws-php4cgi.reg file (look into the SAPI dir) to reflect the location of your php.exe. Forward slashes should be escaped, for example: [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\w3svc\parameters\Script Map] ".php"="c:\\php\\php.exe"

  • In the PWS Manager, right click on a given directory you want to add PHP support to, and select Properties. Check the 'Execute' checkbox, and confirm.

If you choose the ISAPI module, do the following:

  • Edit the enclosed pws-php4isapi.reg file (look into the SAPI dir) to reflect the location of your php4isapi.dll. Forward slashes should be escaped, for example: [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\w3svc\parameters\Script Map] ".php"="c:\\php\\sapi\\php4isapi.dll"

  • In the PWS Manager, right click on a given directory you want to add PHP support to, and select Properties. Check the 'Execute' checkbox, and confirm.


Windows NT/2000/XP and IIS 4 or newer

To install PHP on an NT/2000/XP Server running IIS 4 or newer, follow these instructions. You have two options to set up PHP, using the CGI binary (php.exe) or with the ISAPI module.

In either case, you need to start the Microsoft Management Console (may appear as 'Internet Services Manager', either in your Windows NT 4.0 Option Pack branch or the Control Panel=>Administrative Tools under Windows 2000/XP). Then right click on your Web server node (this will most probably appear as 'Default Web Server'), and select 'Properties'.

If you want to use the CGI binary, do the following:

  • Under 'Home Directory', 'Virtual Directory', or 'Directory', click on the 'Configuration' button, and then enter the App Mappings tab.

  • Click Add, and in the Executable box, type: c:\php\php.exe (assuming that you have unziped PHP in c:\php\).

  • In the Extension box, type the file name extension you want associated with PHP scripts. Leave 'Method exclusions' blank, and check the Script engine checkbox. You may also like to check the 'check that file exists' box - for a small performance penalty, IIS (or PWS) will check that the script file exists and sort out authentication before firing up php. This means that you will get sensible 404 style error messages instead of cgi errors complaining that php did not output any data.

    You must start over from the previous step for each extension you want associated with PHP scripts. .php and .phtml are common, although .php3 may be required for legacy applications.

  • Set up the appropriate security. (This is done in Internet Service Manager), and if your NT Server uses NTFS file system, add execute rights for I_USR_ to the directory that contains php.exe.

To use the ISAPI module, do the following:

  • If you don't want to perform HTTP Authentication using PHP, you can (and should) skip this step. Under ISAPI Filters, add a new ISAPI filter. Use PHP as the filter name, and supply a path to the php4isapi.dll.

  • Under 'Home Directory', click on the 'Configuration' button. Add a new entry to the Application Mappings. Use the path to the php4isapi.dll as the Executable, supply .php as the extension, leave Method exclusions blank, and check the Script engine checkbox.

  • Stop IIS completely (NET STOP iisadmin)

  • Start IIS again (NET START w3svc)


Servers-Netscape and iPlanet

This section contains notes and hints specific to Netscape and iPlanet installs of PHP, both for Sun Solaris and Windows versions.

You can find more information about setting up PHP for the Netscape Enterprise Server here: http://benoit.noss.free.fr/php/install-php4.html


Installing PHP with Netscape on Sun Solaris

To build PHP with NES or iPlanet web servers, enter the proper install directory for the --with-nsapi = DIR option. The default directory is usually /opt/netscape/suitespot/. Please also read /php-xxx-version/sapi/nsapi/nsapi-readme.txt.

Exemplo 3-7. Installation Example for Netscape Enterprise on Solaris

Instructions for Sun Solaris 2.6 with Netscape Enterprise Server 3.6
From: bhager@invacare.com

1. Install the following packages from www.sunfreeware.com or another
download site: 

    flex-2_5_4a-sol26-sparc-local 
    gcc-2_95_2-sol26-sparc-local 
    gzip-1.2.4-sol26-sparc-local 
    perl-5_005_03-sol26-sparc-local 
    bison-1_25-sol26-sparc-local 
    make-3_76_1-sol26-sparc-local 
    m4-1_4-sol26-sparc-local 
    autoconf-2.13 
    automake-1.4 
    mysql-3.23.24-beta (if you want mysql support) 
    tar-1.13 (GNU tar) 

2. Make sure your path includes the proper directories
    PATH=.:/usr/local/bin:/usr/sbin:/usr/bin:/usr/ccs/bin 
    export PATH 

3. gunzip php-x.x.x.tar.gz (if you have a .gz dist, otherwise go to 4) 
4. tar xvf php-x.x.x.tar 
5. cd ../php-x.x.x 

6. For the following step, make sure /opt/netscape/suitespot/ is where
   your netscape server is installed. Otherwise, change to correct path:
    /configure --with-mysql=/usr/local/mysql --with-nsapi=/opt/netscape/suitespot/ --enable-track-vars --enable-libgcc 
7. make 
8. make install
After performing the base install and reading the appropriate readme file, you may need to performs some additional configuration steps.

Firstly you may need to add some paths to the LD_LIBRARY_PATH environment for Netscape to find all the shared libs. This can best done in the start script for your Netscape server. Windows users can probably skip this step. The start script is often located in: /path/to/server/https-servername/start

You may also need to edit the configuration files that are located in:/path/to/server/https-servername/config/.

Exemplo 3-8. Configuration Example for Netscape Enterprise

Configuration Instructions for Netscape Enterprise Server
From: bhager@invacare.com

1. Add the following line to mime.types:
    type=magnus-internal/x-httpd-php exts=php

2. Add the following to obj.conf, shlib will vary depending on
    your OS, for unix it will be something like
    /opt/netscape/suitespot/bin/libphp4.so.

    You should place the following lines after mime types init.
    Init fn="load-modules" funcs="php4_init,php4_close,php4_execute,php4_auth_trans" shlib="/php4/nsapiPHP4.dll"
    Init fn=php4_init errorString="Failed to initialize PHP!"

    <object name="default">
    . 
    . 
    . 
    .#NOTE this next line should happen after all 'ObjectType' and before all 'AddLog' lines 
    Service fn="php4_execute" type="magnus-internal/x-httpd-php" 
    . 
    . 
    </Object>


    <Object name="x-httpd-php"> 
    ObjectType fn="force-type" type="magnus-internal/x-httpd-php" 
    Service fn=php4_execute 
    </Object> 


    Authentication configuration 

    PHP authentication cannot be used with any other authentication. ALL AUTHENTICATION IS 
    PASSED TO YOUR PHP SCRIPT. To configure PHP Authentication for the entire server, add 
    the following line: 

    <Object name="default"> 
    AuthTrans fn=php4_auth_trans 
    . 
    . 
    . 
    . 
    </Object> 

    To use PHP Authentication on a single directory, add the following: 

    <Object ppath="d:\path\to\authenticated\dir\*"> 
    AuthTrans fn=php4_auth_trans 
    </Object>

If you are running Netscape Enterprise 4.x, then you should use the following:

Exemplo 3-9. Configuration Example for Netscape Enterprise 4.x

Place these lines after the mime types init, and everything else is similar
to the example configuration above.
From: Graeme Hoose (GraemeHoose@BrightStation.com)

Init fn="load-modules" shlib="/path/to/server4/bin/libphp4.so" funcs="php4_init,php4_close,php4_execute,php4_auth_trans"
Init fn="php4_init" LateInit="yes"

Installing PHP with Netscape on Windows

To Install PHP as CGI (for Netscape Enterprise Server, iPlanet, perhaps Fastrack), do the following:

  • Copy php4ts.dll to your systemroot (the directory where you installed windows)

  • Make a file association from the command line. Type the following two lines:
    assoc .php=PHPScript
    ftype PHPScript=c:\php\php.exe %1 %*

  • In the Netscape Enterprise Administration Server create a dummy shellcgi directory and remove it just after (this step creates 5 important lines in obj.conf and allow the web server to handle shellcgi scripts).

  • In the Netscape Enterprise Administration Server create a new mime type (Category: type, Content-Type: magnus-internal/shellcgi, File Suffix:php).

  • Do it for each web server instance you want php to run

More details about setting up PHP as a CGI executable can be found here: http://benoit.noss.free.fr/php/install-php.html

To Install PHP as NSAPI (for Netscape Enterprise Server, iPlanet, perhaps Fastrack, do the following:

  • Copy php4ts.dll to your systemroot (the directory where you installed windows)

  • Make a file association from the command line. Type the following two lines:
    assoc .php=PHPScript
    ftype PHPScript=c:\php\php.exe %1 %*

  • In the Netscape Enterprise Administration Server create a new mime type (Category: type, Content-Type: magnus-internal/x-httpd-php, File Suffix:php).

  • Stop your web service and edit obj.conf. At the end of the Init section, place these two lines (necessarily after mime type init!):
    Init fn="load-modules" funcs="php4_init,php4_close,php4_execute,php4_auth_trans" shlib="c:/php/sapi/php4nsapi.dll"
    Init fn="php4_init" errorString="Failed to initialise PHP!"

  • In The < Object name="default" > section, place this line necessarily after all 'ObjectType' and before all 'AddLog' lines:
    Service fn="php4_execute" type="magnus-internal/x-httpd-php"

  • At the end of the file, create a new object called x-httpd-php, by inserting these lines:
    <Object name="x-httpd-php">
    ObjectType fn="force-type" type="magnus-internal/x-httpd-php"
    Service fn=php4_execute
    </Object>

  • Restart your web service and apply changes

  • Do it for each web server instance you want PHP to run

More details about setting up PHP as an NSAPI filter can be found here: http://benoit.noss.free.fr/php/install-php4.html


Servers-OmniHTTPd Server

This section contains notes and hints specific to OmniHTTPd.


OmniHTTPd 2.0b1 and up for Windows

You need to complete the following steps to make PHP work with OmniHTTPd. This is a CGI executable setup. SAPI is supported by OmniHTTPd, but some tests have shown that it is not so stable to use PHP as an ISAPI module.

  • Step 1: Install OmniHTTPd server.

  • Step 2: Right click on the blue OmniHTTPd icon in the system tray and select Properties

  • Step 3: Click on Web Server Global Settings

  • Step 4: On the 'External' tab, enter: virtual = .php | actual = c:\path-to-php-dir\php.exe, and use the Add button.

  • Step 5: On the Mime tab, enter: virtual = wwwserver/stdcgi | actual = .php, and use the Add button.

  • Step 6: Click OK

Repeat steps 2 - 6 for each extension you want to associate with PHP.

Nota: Some OmniHTTPd packages come with built in PHP support. You can choose at setup time to do a custom setup, and uncheck the PHP component. We recommend you to use the latest PHP binaries. Some OmniHTTPd servers come with PHP 4 beta distributions, so you should choose not to set up the built in support, but install your own. If the server is already on your machine, use the Replace button in Step 4 and 5 to set the new, correct information.


Servers-Oreilly Website Pro

This section contains notes and hints specific to Oreilly Website Pro.


Oreilly Website Pro 2.5 and up for Windows

This list describes how to set up the PHP CGI binary or the ISAPI module to work with Oreilly Website Pro on Windows.

  • Edit the Server Properties and select the tab "Mapping".

  • From the List select "Associations" and enter the desired extension (.php) and the path to the CGI exe (ex. c:\php\php.exe) or the ISAPI DLL file (ex. c:\php\sapi\php4isapi.dll).

  • Select "Content Types" add the same extension (.php) and enter the content type. If you choose the CGI executable file, enter 'wwwserver/shellcgi', if you choose the ISAPI module, enter 'wwwserver/isapi' (both without quotes).


Servers-Xitami

This section contains notes and hints specific to Xitami.


Xitami for Windows

This list describes how to set up the PHP CGI binary to work with Xitami on Windows.

  • Make sure the webserver is running, and point your browser to xitamis admin console (usually http://127.0.0.1/admin), and click on Configuration.

  • Navigate to the Filters, and put the extension which PHP should parse (i.e. .php) into the field File extensions (.xxx).

  • In Filter command or script put the path and name of your php executable i.e. c:\php\php.exe.

  • Press the 'Save' icon.

  • Restart the server to reflect changes.


Servers-Other web servers

PHP can be built to support a large number of web servers. Please see Server-related options for a full list of server-related configure options. The PHP CGI binaries are compatible with almost all webservers supporting the CGI standard.


Problems?

Read the FAQ

Some problems are more common than others. The most common ones are listed in the PHP FAQ, part of this manual.


Other problems

If you are still stuck, someone on the PHP installation mailing list may be able to help you. You should check out the archive first, in case someone already answered someone else who had the same problem as you. The archives are available from the support page on http://www.php.net/. To subscribe to the PHP installation mailing list, send an empty mail to php-install-subscribe@lists.php.net. The mailing list address is php-install@lists.php.net.

If you want to get help on the mailing list, please try to be precise and give the necessary details about your environment (which operating system, what PHP version, what web server, if you are running PHP as CGI or a server module, etc.), and preferably enough code to make others able to reproduce and test your problem.


Bug reports

If you think you have found a bug in PHP, please report it. The PHP developers probably don't know about it, and unless you report it, chances are it won't be fixed. You can report bugs using the bug-tracking system at http://bugs.php.net/. Please do not send bug reports in mailing list or personal letters. The bug system is also suitable to submit feature requests.

Read the How to report a bug document before submitting any bug reports!


Complete list of configure options

Nota: These are only used at compile time. If you want to alter PHP's runtime configuration, please see the chapter on Configuration.

The following is a complete list of options supported by PHP 4 configure scripts (as of 4.1.0), used when compiling in Unix-like environments. Some are available in PHP 3, some in PHP 4, and some in both. This is not noted yet.


Configure Options in PHP 4

Nota: These options are only used in PHP 4 as of PHP 4.1.0. Some are available in older versions of PHP 4, some even in PHP 3, some only in PHP 4.1.0. If you want to compile an older version, some options will probably not be available.


Database options

--with-db

Include old xDBM support (deprecated).

--enable-dba=shared

Build DBA as a shared module.

--with-gdbm[=DIR]

Include GDBM support.

--with-ndbm[=DIR]

Include NDBM support.

--with-db2[=DIR]

Include Berkeley DB2 support.

--with-db3[=DIR]

Include Berkeley DB3 support.

--with-dbm[=DIR]

Include DBM support.

--with-cdb[=DIR]

Include CDB support.

--enable-dbase

Enable the bundled dbase library.

--with-dbplus

Include dbplus support.

--enable-dbx

Enable dbx.

--with-fbsql[=DIR]

Include FrontBase support. DIR is the FrontBase base directory.

--enable-filepro

Enable the bundled read-only filePro support.

--with-fribidi[=DIR]

Include fribidi support (requires FriBidi >=0.1.12). DIR is the fribidi installation directory - default /usr/local/.

--with-informix[=DIR]

Include Informix support. DIR is the Informix base install directory, defaults to nothing.

--with-ingres[=DIR]

Include Ingres II support. DIR is the Ingres base directory (default /II/ingres).

--with-interbase[=DIR]

Include InterBase support. DIR is the InterBase base install directory, defaults to /usr/interbase.

--with-msql[=DIR]

Include mSQL support. DIR is the mSQL base install directory, defaults to /usr/local/Hughes.

--with-mysql[=DIR]

Include MySQL support. DIR is the MySQL base directory. If unspecified, the bundled MySQL library will be used.

--with-oci8[=DIR]

Include Oracle-oci8 support. Default DIR is ORACLE_HOME.

--with-adabas[=DIR]

Include Adabas D support. DIR is the Adabas base install directory, defaults to /usr/local.

--with-sapdb[=DIR]

Include SAP DB support. DIR is SAP DB base install directory, defaults to /usr/local.

--with-solid[=DIR]

Include Solid support. DIR is the Solid base install directory, defaults to /usr/local/solid.

--with-ibm-db2[=DIR]

Include IBM DB2 support. DIR is the DB2 base install directory, defaults to /home/db2inst1/sqllib.

--with-empress[=DIR]

Include Empress support. DIR is the Empress base install directory, defaults to $EMPRESSPATH. From PHP4, this option only supports Empress Version 8.60 and above.

--with-empress-bcs[=DIR]

Include Empress Local Access support. DIR is the Empress base install directory, defaults to $EMPRESSPATH. From PHP4, this option only supports Empress Version 8.60 and above.

--with-birdstep[=DIR]

Include Birdstep support. DIR is the Birdstep base install directory, defaults to /usr/local/birdstep.

--with-custom-odbc[=DIR]

Include a user defined ODBC support. The DIR is ODBC install base directory, which defaults to /usr/local. Make sure to define CUSTOM_ODBC_LIBS and have some odbc.h in your include dirs. E.g., you should define following for Sybase SQL Anywhere 5.5.00 on QNX, prior to run configure script: CPPFLAGS="-DODBC_QNX -DSQLANY_BUG" LDFLAGS=-lunix CUSTOM_ODBC_LIBS="-ldblib -lodbc".

--with-iodbc[=DIR]

Include iODBC support. DIR is the iODBC base install directory, defaults to /usr/local.

--with-esoob[=DIR]

Include Easysoft OOB support. DIR is the OOB base install directory, defaults to /usr/local/easysoft/oob/client.

--with-unixODBC[=DIR]

Include unixODBC support. DIR is the unixODBC base install directory, defaults to /usr/local.

--with-openlink[=DIR]

Include OpenLink ODBC support. DIR is the OpenLink base install directory, defaults to /usr/local. This is the same as iODBC.

--with-dbmaker[=DIR]

Include DBMaker support. DIR is the DBMaker base install directory, defaults to where the latest version of DBMaker is installed (such as /home/dbmaker/3.6).

--with-oracle[=DIR]

Include Oracle-oci7 support. Default DIR is ORACLE_HOME.

--with-ovrimos[=DIR]

Include Ovrimos SQL Server support. DIR is the Ovrimos' libsqlcli install directory.

--with-pgsql[=DIR]

Include PostgreSQL support. DIR is the PostgreSQL base install directory, defaults to /usr/local/pgsql. Set DIR to shared to build as a dl, or shared,DIR to build as a dl and still specify DIR.

--with-sybase[=DIR]

Include Sybase-DB support. DIR is the Sybase home directory, defaults to /home/sybase.

--with-sybase-ct[=DIR]

Include Sybase-CT support. DIR is the Sybase home directory. Defaults to /home/sybase.

--disable-unified-odbc

Disable unified ODBC support. Only applicable if iODBC, Adabas, Solid, Velocis or a custom ODBC interface is enabled. PHP 3 only!


Graphics options

--with-gd[=DIR]

Include GD support (DIR is GD's install dir). Set DIR to shared to build as a dl, or shared,DIR to build as a dl and still specify DIR.

--enable-gd-native-ttf

GD: Enable TrueType string function in gd.

--with-jpeg-dir=DIR

GD: Set the path to libjpeg install prefix.

--with-png-dir=DIR

GD: Set the path to libpng install prefix.

--with-xpm-dir=DIR

GD: Set the path to libXpm install prefix.

--with-freetype-dir=DIR

GD: Set the path to freetype2 install prefix.

--with-ttf[=DIR]

GD: Include FreeType 1.x support.

--with-t1lib[=DIR]

GD: Include T1lib support.

--with-cpdflib[=DIR]

Include cpdflib support (requires cpdflib >= 2). DIR is the cpdfllib install directory, defaults to /usr.

--with-jpeg-dir[=DIR]

jpeg dir for cpdflib 2.x.

--with-tiff-dir[=DIR]

tiff dir for cpdflib 2.x.

--with-pdflib[=DIR]

Include PDFlib support. DIR is the pdflib base install directory, defaults to /usr/local. Set DIR to shared to build as dl, or shared,DIR to build as dl and still specify DIR.

--with-jpeg-dir[=DIR]

PDFLIB: define libjpeg install directory.

--with-png-dir[=DIR]

PDFLIB: define libpng install directory.

--with-tiff-dir[=DIR]

PDFLIB: define libtiff install directory.

--with-swf[=DIR]

Include swf support.

--without-gd

Disable GD support. PHP 3 only!

--with-imagick

Include ImageMagick support. DIR is the install directory, and if left out, PHP will try to find it on its own. [experimental]. PHP 3 only!

--with-ming[=DIR]

Include ming support.


Misc options

--enable-force-cgi-redirect

Enable the security check for internal server redirects. You should use this if you are running the CGI version with Apache.

--enable-discard-path

If this is enabled, the PHP CGI binary can safely be placed outside of the web tree and people will not be able to circumvent .htaccess security.

--with-fastcgi=SRCDIR

Build PHP as FastCGI application.

--enable-debug

Compile with debugging symbols.

--with-layout=TYPE

Sets how installed files will be laid out. Type is one of PHP (default) or GNU.

--with-pear=DIR

Install PEAR in DIR (default PREFIX/lib/php).

--without-pear

Do not install PEAR.

--with-openssl[=DIR]

Include OpenSSL support (requires OpenSSL >= 0.9.5).

--enable-sigchild

Enable PHP's own SIGCHLD handler.

--disable-rpath

Disable passing additional runtime library search paths.

--enable-libgcc

Enable explicitly linking against libgcc.

--enable-dmalloc

Enable dmalloc.

--enable-php-streams

Include experimental php streams. Do not use unless you are testing the code!

--with-zlib-dir=<DIR>

Define the location of zlib install directory.

--with-zlib[=DIR]

Include zlib support (requires zlib >= 1.0.9). DIR is the zlib install directory.

--with-aspell[=DIR]

Include ASPELL support.

--enable-bcmath

Enable bc style precision math functions.

--with-bz2[=DIR]

Include BZip2 support.

--enable-calendar

Enable support for calendar conversion.

--with-ccvs[=DIR]

Include CCVS support.

--with-crack[=DIR]

Include crack support.

--enable-ctype

Enable ctype support.

--with-curl[=DIR]

Include CURL support.

--with-cybercash[=DIR]

Include CyberCash support. DIR is the CyberCash MCK install directory.

--with-cybermut[=DIR]

Include CyberMut (French Credit Mutuel telepaiement).

--with-cyrus

Include cyrus IMAP support.

--enable-exif

Enable exif support.

--with-fdftk[=DIR]

Include fdftk support.

--enable-ftp

Enable FTP support.

--with-gettext[=DIR]

Include GNU gettext support. DIR is the gettext install directory, defaults to /usr/local.

--with-gmp

Include gmp support.

--with-hyperwave

Include Hyperwave support.

--with-icap[=DIR]

Include ICAP support.

--with-iconv[=DIR]

Include iconv support.

--with-imap[=DIR]

Include IMAP support. DIR is the c-client install prefix.

--with-kerberos[=DIR]

IMAP: Include Kerberos support. DIR is the Kerberos install dir.

--with-imap-ssl[=DIR]

IMAP: Include SSL support. DIR is the OpenSSL install dir.

--with-ircg-config

Path to the ircg-config script.

--with-ircg

Include ircg support.

--with-java[=DIR]

Include Java support. DIR is the base install directory for the JDK. This extension can only be built as a shared dl.

--with-ldap[=DIR]

Include LDAP support. DIR is the LDAP base install directory.

--enable-mailparse

Enable mailparse support.

--enable-mbstring

Enable multibyte string support.

--enable-mbstr-enc-trans

Enable japanese encoding translation.

--with-mcal[=DIR]

Include MCAL support.

--with-mcrypt[=DIR]

Include mcrypt support. DIR is the mcrypt install directory.

--with-mhash[=DIR]

Include mhash support. DIR is the mhash install directory.

--with-mnogosearch[=DIR]

Include mnoGoSearch support. DIR is the mnoGoSearch base install directory, defaults to /usr/local/mnogosearch.

--with-muscat[=DIR]

Include muscat support.

--with-ncurses

Include ncurses support.

--enable-pcntl

Enable experimental pcntl support (CGI ONLY!)

--without-pcre-regex

Do not include Perl Compatible Regular Expressions support. Use --with-pcre-regex=DIR to specify DIR where PCRE's include and library files are located, if not using bundled library.

--with-pfpro[=DIR]

Include Verisign Payflow Pro support.

--disable-posix

Disable POSIX-like functions.

--with-pspell[=DIR]

Include PSPELL support.

--with-qtdom

Include QtDOM support (requires Qt >= 2.2.0).

--with-libedit[=DIR]

Include libedit readline replacement.

--with-readline[=DIR]

Include readline support. DIR is the readline install directory.

--with-recode[=DIR]

Include recode support. DIR is the recode install directory.

--with-satellite[=DIR]

Enable CORBA support via Satellite (EXPERIMENTAL) DIR is the base directory for ORBit.

--with-mm[=DIR]

Include mm support for session storage.

--enable-trans-sid

Enable transparent session id propagation.

--disable-session

Disable session support.

--enable-shmop

Enable shmop support.

--with-snmp[=DIR]

Include SNMP support. DIR is the SNMP base install directory, defaults to searching through a number of common locations for the snmp install. Set DIR to shared to build as a dl, or shared,DIR to build as a dl and still specify DIR.

--enable-ucd-snmp-hack

Enable UCD SNMP hack.

--enable-sockets

Enable sockets support.

--with-regex=TYPE

regex library type: system, apache, php.

--with-system-regex

Use system regex library (deprecated).

--enable-sysvsem

Enable System V semaphore support.

--enable-sysvshm

Enable the System V shared memory support.

--with-vpopmail[=DIR]

Include vpopmail support.

--with-tsrm-pthreads

Use POSIX threads (default).

--enable-shared[=PKGS]

Build shared libraries [default=yes].

--enable-static[=PKGS]

Build static libraries [default=yes].

--enable-fast-install[=PKGS]

Optimize for fast installation [default=yes].

--with-gnu-ld

Assume the C compiler uses GNU ld [default=no].

--disable-libtool-lock

Avoid locking (might break parallel builds).

--with-pic

Try to use only PIC/non-PIC objects [default=use both].

--with-yaz[=DIR]

Include YAZ support (ANSI/NISO Z39.50). DIR is the YAZ bin install directory.

--enable-memory-limit

Compile with memory limit support.

--disable-url-fopen-wrapper

Disable the URL-aware fopen wrapper that allows accessing files via HTTP or FTP.

--enable-versioning

Export only required symbols. See INSTALL for more information.

--disable-bcmath

Compile without bc style precision math functions. PHP 3 only!

--with-imsp[=DIR]

Include IMSp support (DIR is IMSP's include dir and libimsp.a dir). PHP 3 only!

--with-ftp

Include FTP support. PHP 3 only!

--with-mck[=DIR]

Include Cybercash MCK support. DIR is the cybercash mck build directory, defaults to /usr/src/mck-3.2.0.3-linux for help look in extra/cyberlib. PHP 3 only!

--disable-overload

Disable user-space object overloading support. PHP 3 only!

--enable-yp

Include YP support. PHP 3 only!

--with-zip

Include ZIP support (requires zziplib >= 0.10.6). PHP 3 only!

--with-mod-dav=DIR

Include DAV support through Apache's mod_dav, DIR is mod_dav's installation directory (Apache module version only!) PHP 3 only!

--enable-debugger

Compile with remote debugging functions. PHP 3 only!

--enable-versioning

Take advantage of versioning and scoping provided by Solaris 2.x and Linux. PHP 3 only!


PHP options

--enable-maintainer-mode

Enable make rules and dependencies not useful (and sometimes confusing) to the casual installer.

--with-config-file-path=PATH

Sets the path in which to look for php.ini, defaults to PREFIX/lib.

--enable-safe-mode

Enable safe mode by default.

--with-exec-dir[=DIR]

Only allow executables in DIR when in safe mode defaults to /usr/local/php/bin.

--enable-magic-quotes

Enable magic quotes by default.

--disable-short-tags

Disable the short-form <? start tag by default.


Server options

--with-aolserver=DIR

Specify path to the installed AOLserver.

--with-apxs[=FILE]

Build shared Apache module. FILE is the optional pathname to the Apache apxs tool; defaults to apxs. Make sure you specify the version of apxs that is actually installed on your system and NOT the one that is in the apache source tarball.

--with-apache[=DIR]

Build Apache module. DIR is the top-level Apache build directory, defaults to /usr/local/apache.

--with-mod_charset

Enable transfer tables for mod_charset (Rus Apache).

--with-apxs2[=FILE]

Build shared Apache 2.0 module. FILE is the optional pathname to the Apache apxs tool; defaults to apxs.

--with-fhttpd[=DIR]

Build fhttpd module. DIR is the fhttpd sources directory, defaults to /usr/local/src/fhttpd.

--with-isapi=DIR

Build PHP as an ISAPI module for use with Zeus.

--with-nsapi=DIR

Specify path to the installed Netscape Server.

--with-phttpd=DIR

No information yet.

--with-pi3web=DIR

Build PHP as a module for use with Pi3Web.

--with-roxen=DIR

Build PHP as a Pike module. DIR is the base Roxen directory, normally /usr/local/roxen/server.

--enable-roxen-zts

Build the Roxen module using Zend Thread Safety.

--with-servlet[=DIR]

Include servlet support. DIR is the base install directory for the JSDK. This SAPI prereqs the java extension must be built as a shared dl.

--with-thttpd=SRCDIR

Build PHP as thttpd module.

--with-tux=MODULEDIR

Build PHP as a TUX module (Linux only).


XML options

--with-dom[=DIR]

Include DOM support (requires libxml >= 2.4.2). DIR is the libxml install directory, defaults to /usr.

--disable-xml

Disable XML support using bundled expat lib.

--with-expat-dir=DIR

XML: external libexpat install dir.

--with-xmlrpc[=DIR]

Include XMLRPC-EPI support.

--enable-wddx

Enable WDDX support.


Capítulo 4. Configuração

O arquivo de configuração

O arquivo de configuração (chamado php3.ini no PHP 3.0, e simplesmente php.ini no PHP 4.0) é lido quando o PHP inicia. Para as versões de módulos de servidor do PHP, isso só acontece uma vez quando o servidor é iniciado. Para as versões CGI e CLI acontece em cada execução.

A localização padrão do php.ini é uma opção de compilação (veja a FAQ correspondente), mas pode ser modificada para as versões CGI e CLI com o opcional -c (mais informações em utilizando o PHP na linha de comando). Você também pode utilizar a variavável ambiente PHPRC para colocar caminhos adicionais para procura do php.ini.

Note que nem todas as diretivas do PHP estão documentadas a seguir. Para uma lista completa, leia o seu próprio e bem comentado arquivo php.ini. Você também pode ver a versão mais atualizada diretamente do CVS.

Nota: O valor default da diretiva register_globals mudou de on para off a partir do PHP 4.2.0.

Exemplo 4-1. php.ini example

; qualquer texto depois de um ponto e vírgula não delimitado (;) é ignorado
[php] ; marcadores de seção (texto entre colchetes) também são ignorados
; Valores booleanos podem ser:
;    true, on, yes
; ou false, off, no, none
register_globals = off
magic_quotes_gpc = yes

; você pode delimitar strings com aspas
include_path = ".:/usr/local/lib/php"

; barras invertidas são tratadas da mesma maneira que outros caracteres
include_path = ".;c:\php\lib"

Quando usando o PHP como um módulo do Apache, você também pode mudar as definições de configuração usando diretivas na configuração dos arquivos Apache e dos arquivos .htaccess (Você irá precisar de privilégios "AllowOverride Options" ou "AllowOverride All")

Com o PHP 3.0, existem diretivas Apache que correspondem a cada definição de configuração no nome php3.ini, exceto o nome predefinido por "php3_".

Com o PHP 4.0, existem várias diretivas do Apache que lhe permitem mudar a configuração PHP dentro do arquivo de configuração do Apache.

php_value nome valor

Isso define o valor da variável especificada.

php_flag nome on|off

Isto é usado para definir uma opção de configuração Booleana.

php_admin_value nome valor

Isto define o valor da variável especificada. Definições de configuração "Admin" só podem ser definidas a partir dos arquivos principais de configuração do Apache, e não dos arquivos .htaccess.

php_admin_flag nome on|off

Isto é usado para definir uma opção de configuração Booleana.

Exemplo 4-2. Exemplo de configuração do Apache

<IfModule mod_php4.c>
  php_value include_path ".:/usr/local/lib/php"
  php_flag safe_mode on
</IfModule>
<IfModule mod_php3.c>
  php3_include_path ".:/usr/local/lib/php"
  php3_safe_mode on
</IfModule>

Nota: Constantes PHP não existem fora do PHP. Por exemplo, no httpd.conf não adianta utilizar-se de constantes PHP como E_ALL ou E_NOTICE para configurar a diretiva error_reporting, pois elas não tem nenhum sentido e serão avaliados como 0. Nesses casos, utilize seus valores de bit. Essas constantes só podem ser utilizadas dentro do php.ini

Você pode visualizar as definições dos valores de configuração na saída de phpinfo(). Você também pode acessar os valores de definições de configuração originais usando ini_get() ou get_cfg_var().


Diretivas Gerais de Configuração

allow_url_fopen boolean

Esta opção habilita o URL-aware fopen wrappers que possibilitam o acesso a objetos URL como arquivos. Wrappers padrão são fornecidos para o acesso de arquivos remotos usando os protocolos FTP ou HTTP, algumas extensões como zlib podem registrar wrappers adicionais.

Nota: Esta opção foi introduzida imediatamente após o lançamento da versão 4.0.3. Para versões superiores incluindo a 4.0.3, você só pode desabilitar essa função na compilação usando o argumento --disable-url-fopen-wrapper na configuração (configure).

Atenção

No Windows, as seguintes funções não suportam o acesso a arquivos remotos: include(), include_once(), require() e require_once().

asp_tags booleano

Habilita o uso de tags no estilo ASP <% %> em adição às tags <?php ?> usuais. Isso inclui a impressão resumida de valor de variável <%= $valor %>. Para maiores informações veja Escapando do HTML.

Nota: O suporte para tags no estilo ASP foi adicionado na versão 3.0.4.

auto_append_file string

Especifica o nome do arquivo que é automaticamente interpretado após o arquivo principal. O arquivo é incluído como se tivesse sido chamado com a função include(), logo include_path é usado.

O valor especial none desabilita o auto-appending.

Nota: Se o script é finalizado com exit(), auto-append não ocorrerá.

auto_prepend_file string

Especifica o nome do arquivo que é automaticamente interpretado antes do arquivo principal. O arquivo é incluído como se tivesse sido chamado com a função include(), logo include_path é usado.

O valor especial none desabilita o auto-prepending.

disable_functions string

Esta diretiva permite a você desabilitar certas funções por motivos de segurança. Informe uma lista de nomes de funções separados por vírgula. disable_functions não é afetado pelo Safe Mode.

Esta diretiva só pode ser configurada no php.ini Por exemplo, você não pode modificá-la no httpd.conf.

display_errors booleano

Isso determina se os erros deve ser impressos na tela como parte da saída HTML ou não.

doc_root string

O "root directory" do PHP no servidor. Só é usado se não estiver vazio. Se o PHP é configurado com safe mode, nenhum arquivo acima desse diretório é acessível.

engine booleano

Essa diretiva só é realmente útil com a versão de módulo Apache do PHP. É usado por sites que queiram habilitar ou desabiliar a interpretação PHP num servidor por diretório ou por domínio virtual. Colocando engine off nos devidos lugares no arquivo httpd.conf, o PHP pode ser habilitado ou desabilitado.

error_log string

Nome do arquivo onde erros de script devem ser guardados. Se o valor especial syslog for usado, os erros serão enviados para o logger do sistema. No UNIX, isto significa syslog(3) e no Windows NT isto significa o event log. O system logger não é suportado no Windows 95.

error_reporting inteiro

Define o nível de publicação de erros. O parâmetro é um inteiro representado um campo de bits. Adicione os valores que você queira do nível de publicação de erros.

Tabela 4-1. Níveis de Publicação de Erros

valor do bithabilita publicação
1erros normais
2avisos normais
4erros de interpretação
8avisos não críticos relacionados ao estilo
O valor padrão para esta diretiva é 7 (erros normais, avisos normais e erros de interpretação são mostrados).

html_errors booleano

Desabilita tags HTML nas mensagens de erro.

open_basedir string

Limita os arquios que podem ser abertos pelo PHP à arvore de diretórios especificada.

Quando um script tenta abrir um arquivo com, por exemplo, fopen or gzopen, a localização desse arquivo é checada. Quando o arquivo está fora da árvore de diretórios especificada, o PHP irá se recusar a abrí-lo. Todos os links simbólicos são resolvidos, logo não é possível evitar essa restrição com um symlink.

O valor especial . indica que o diretório onde o script está armazenado será usado como o diretório base.

No Windows, separe os diretórios com ponto-e-vírgula. Em todos os outros sistemas, separe os diretórios com dois pontos. Como um módulo Apache, os caminhos de open_basedir de diretórios pais agora são automaticamente herdados.

A restrição imposta com open_basedir é na verdade um prefixo, e não um nome de diretório. Isso significa que "open_basedir = /dir/incl" também permite acesso a "/dir/include" e "/dir/incls" se eles existem. Quando você deseja restringir o acesso somente ao diretório específico, o termine com uma barra. Por exemplo: "open_basedir = /dir/incl/"

Nota: O supporte para múltiplos diretórios foi adicionado na versão 3.0.7.

O padrão é permitir que todos os arquivos sejam abertos.

gpc_order string

Define a ordem de interpretação da variável GET/POST/COOKIE . A definição padrão para essa diretiva é "GPC". Definindo-a para "GP", por exemplo, fará com que o PHP ignore completamente cookies e irá sobescrever quaisquer variáveis enviadas com o método GET por variáveis do método POST com o mesmo nome.

Note que esta opção não está disponível no PHP 4. Use variables_order ao invez.

variables_order string

Define a ordem de prioridade das variáveis EGPCS (Environment, GET, POST, Cookie, Server). A definição padrão para essa diretiva é "EGPCS". Definindo como "GP", por exemplo, fará com que o PHP ignore completamente as variáveis de ambiente (environment) cookies e server, e sobrescrever qualquer variavel do metodo GET com variaveis do modo POST com o mesmo nome.

Veja também register_globals.

ignore_user_abort boleano

TRUE por default. Se alterado para FALSE, os scripts serão termiandos tão logo quanto ele tentar imprimir algo e descobrir que o usuário cancelou a conexão.

Veja também: ignore_user_abort().

implicit_flush booleano

FALSE por default. Alterando para TRUE, diz ao PHP para chamar as rotinas de impressão/output automaticamente depois de cada comando de saída. Isto é equivalente a chamar a função flush() depois de cada chamada a print() ,echo() ou depois de qualquer outro bloco HTML.

Quanto utilizando o PHP em um ambiente Web, ativar esta opção tem sérias implicações na performance e é geralmente recomendado para propósitos de debug. Este valor é TRUE por default quando operando sobre a CLI SAPI.

include_path string

Especifica uma lista de diretórios onde as funções require(), include() e fopen_with_path() procuram por arquivos. O formato é o mesmo da variável de ambiente PATH do sistema: uma lista de diretórios separados por dois pontos no UNIX ou ponto-e-vírgula no Windows.

Exemplo 4-3. include_path no UNIX

include_path=.:/home/httpd/php-lib

Exemplo 4-4. include_path no Windows

include_path=".;c:\www\phplib"
O valor padrão para essa diretiva é . (apenas o diretório atual).

log_errors booleano

Diz se as mensagens de erro devem ser salvas no arquivo de log do servidor. Logo, esta opção é específica por servidor.

magic_quotes_gpc booleano

Define o estado de magic_quotes para as operações GPC (Get/Post/Cookie). Quando os magic_quotes estão habilitados (on), todas ' (aspas simples), " (aspas duplas), \ (barra invetida) e NULL's são automaticamente prefixadas (escaped) com a barra invertida. Se magic_quotes_sybase também está habilitado (on), uma aspa simples é escapada como aspa simples ao invés da barra invertida.

magic_quotes_runtime booleano

Se magic_quotes_runtime é habilitado, a maioria das funções que retornam dados de qualquer tipo de fonte externa incluindo banco de dados e arquivos de texto terão aspas escapadas com barra invertida. Se magic_quotes_sybase também estiver habilitado (on), aspas simpes são escapadas com outra aspa simples ao invés de barra invertida.

magic_quotes_sybase booleano

Se magic_quotes_sybase também estiver habilitado (on), aspas simples é escapada como aspa simples ao invés de barra invertida se magic_quotes_gpc ou magic_quotes_runtime estiverem habilitados.

max_execution_time inteiro

Isto define a quantide máxima de tempo em segundos que um script é permitido rodar antes de ser finalizado pelo interpretador. Isto previne scripts mal escritos de travar o servidor. O valor padrão é 30.

O tempo máximo de execução não é afetado por qualquer system calls, a função sleep(), etc. Veja a função set_time_limit() para mais detalhes.

memory_limit inteiro

Isto define a quantidade máxima de memória em bytes que um script é capaz de alocar. Isto ajuda a prevenir scripts mal escritos de consumir toda a memória disponível no servidor.

precision inteiro

O número de digitos significativos mostrado em números de ponto flutuante.

register_argc_argv booleano

Diz ao PHP se declara as variaveis argv & argc (que conteriam a informação do GET).

Veja também PHP em linha de comando. Esta diretiva se tornou disponível no PHP 4.0.0 e estava sempre habilitanda antes desta versão.

post_max_size integer

Define o tamanho máximo dos dados informados via POST. Esse parâmetro afeta o upload de arquivos. Para poder receber arquivos grandes, este valor precisa ser maior que o upload_max_filesize.

Se o limite de memória foi informado no script de configuração, memory_limit afetará o upload de arquivos. Ou seja, o memory_limit também precisa maior que post_max_size.

register_globals booleano

Diz quando registrar as variáveis EGPCS (Environment, GET, POST, Cookie, Server) como globais ou não. Por exemplo, se register_globals = on, a URL http://www.example.com/test.php?id=3 irá criar a veriável $id. Ou ainda $DOCUMENT_ROOT a partir $_SERVER['DOCUMENT_ROOT']. Você pode deixar desligá-la se não quiser encher o escopo global de seu script com dados de usuários. Desde o PHP 4.2.0, esta diretiva vem com o default off. Essa preferência é consequência da alteração das Variáveis Predefinidas do PHP, principalmente as superglobals: $_ENV, $_GET, $_POST, $_COOKIE e $_SERVER. Leia o capítulo sobre segurança em Usando register_globals para mais informações.

Observe que register_globals não pode ser modificada no tempo de execução (ini_set()). Entretanto, você pode fazer isso no .htaccess se seu hospedador permitir, como descrito acima. Um exemplo de diretiva no arquivo .htaccess: php_flag register_globals on.

Nota: register_globals é afetada pela diretiva variables_order.

short_open_tag booleano

Diz quando a forma curta (<? ?> das tags do PHP deve ser permitida. Se você quer usar PHP em combinação com XML, você pode desabilita essa opção e usar <?xml ?> inline. De outra forma, você pode imprimir com o PHP assim: <?php echo '<?xml version="1.0"'; ?>. Se desabilitada, você precisá utilizar a forma longa de abertura do PHP (<?php ?>).

Nota: Esta diretiva afeta o atalho <?=, que é idêntico a <? echo. A utilização deste atalho requer short_open_tag ativado.

sql.safe_mode booleano

track_errors booleano

Se habilitado, a última mensagem de erro sempre estará presente na variável global $php_errormsg.

track_vars booleano

Se habilitado, então as variáveis Environment, GET, POST, Cookie, e Server poderão ser encontradas nas matrizes associativas globais $_ENV, $_GET, $_POST, $_COOKIE e $_SERVER.

Note que no PHP 4.0.3, track_vars sempre está habilitada.

upload_tmp_dir string

O diretório temporário usado para armazenar arquivos durante o upload de arquivos. Deve ter permissão de escrita para qualquer usuário em que o PHP esteja sendo rodado.

upload_max_filesize inteiro

O tamanho máximo de um upload de arquivo. O valor é expresso em bytes.

user_dir string

O caminho base do usado como diretório home do usuário para arquivos PHP, por exemplo public_html.

warn_plus_overloading booleano

Se habilitado, esta opção faz com que o PHP imprima um aviso quando o operador mais (+) é usado em variáveis string. Isto é para tornar mais fácil encontrar scripts que precisam ser reescritos para substituir pelo concatenador de strings (.).


Diretivas de Configuração de Modo Seguro (Safe Mode)

safe_mode booleano

Quando habilitar o modo seguro do PHP, leia os capítulos Capítulo de Segurança e Safe Mode para maiores informações

safe_mode_gid booleano

Configura para a utilização do UID (FALSE) ou GID (TRUE) na checagem de acesso dos arquivos. Leia em Safe Mode para maiores informações.

safe_mode_exec_dir string

Se o PHP está sendo usado em modo seguro, system() e as outras funções de execução do sistema recusam-se a iniciar programas que não estejam neste diretório.

safe_mode_include_dir string

A checagem de UID/GID pode ser ignorada quando incluindo arquivos para esse diretório e subdiretórios (o diretório precisa estar em include_path ou o caminho completo precisa ser incluído).


Diretivas ed Configuração do Debugger

debugger.host string

Nome do DNS ou endereço IP da máquina usada pelo debuger.

debugger.port string

Número da porta usada pelo debugger.

debugger.enabled booleano

Se o debugger está habilitado ou não.


Diretivas de Carregamento de Extensão

enable_dl booleano

Esta diretiva só é realmente útil na versão do módulo Apache do PHP. Você pode habilitar ou desabilitar o carregamento dinâmico de extensões PHP com dl() em servidor por virtual ou por diretório.

A principal razão em desabilitar o carregamento dinâmico é a segurança. Com carregamento dinâmico, é possível ignorar todas as restrições safe_mode e open_basedir.

O padrão é permitir o carregamento dinâmico, exceto ao ser usado o modo seguro. No modo seguro, é sempre impossível usar dl().

extension_dir string

Nesse diretório o PHP deve procurar por extensões dinamicamente carregáveis.

extension string

Que extensões dinamicamente carregáveis devem ser carregadas quando o PHP inicia.


Diretivas de Configuração mSQL

msql.allow_persistent booleano

Permite ou não conexões persistentes mSQL.

msql.max_persistent inteiro

O número máximo de conexões persistentes mSQL por processo.

msql.max_links inteiro

O número máximo de conexões mSQL por processo, inclunido conexões persistentes.


Diretivas de Configuração Postgres

pgsql.allow_persistent booleano

Permite ou não conexões persistentes Postgres.

pgsql.max_persistent inteiro

O número máximo de conexões persistentes Postgres por processo.

pgsql.max_links inteiro

O número máximo de conexões Postgres por processo, incluindo conexões persistentes.


Diretivas de Configuração SESAM

sesam_oml string

Nome da biblioteca BS2000 PLAM contendo os módulos de driver SESAM carregáveis. Exigido para usar funções SESAM. A biblioteca BS2000 PLAM deve estar definida ACCESS=READ,SHARE=YES porque ela deve ser lida pelo id de usuário do servidor Apache.

sesam_configfile string

Nome do arquivo de configuração da aplicação SESAM. Exigido ao usar funções SESAM. O arquivo BS2000 deve ser lido pelo id de usuário do servidor apache.

O arquivo de configuração da aplicação irá normalmente conter um arquivo de configuração como (veja o manual de referência SESAM):

CNF=B
NAM=K
NOTYPE

sesam_messagecatalog string

Nome do arquivo de catálogo de mensagem SESAM. Na maioria dos casos, esta diretiva é desnecessária. Apenas se o arquivo de mensagem SESAM não estiver instalado na tabela de arquivo de mensagen do sistema BS2000, pode então ser definida por esta diretiva.

O catálgo de mensagem deve ser definido ACCESS=READ,SHARE=YES porque ele deve ser lido pelo id de usuário do servidor apache.


Diretivas de Configuração Sybase

sybase.allow_persistent booleano

Permite ou não conexões persistentes Sybase.

sybase.max_persistent inteiro

O número máximo de conexões persistentes Sybase por processo.

sybase.max_links inteiro

O número máximo de conexões Sybase por processo, incluindo conexões persistentes.


Diretivas de Configuração Sybase-CT

sybct.allow_persistent booleano

Permite ou não conexões persistentes Sybase-CT. O padrão é permitir.

sybct.max_persistent inteiro

O número máximo de conexões persistentes Sybase-CT por processo. O padrão é -1 que significa ilimitado.

sybct.max_links inteiro

O número máximo de conexões Sybase-CT por processo, incluindo conexões persistentes. O padrão é -1 que significa ilimitado.

sybct.min_server_severity inteiro

Mensagens do servidor muito maiores ou iguais a sybct.min_server_severity serão publicadas como avisos. Este valor também pode ser definido a partir de um script chamando sybase_min_server_severity(). O padrão é 10, que publica erros de informação muito maiores ou maiores.

sybct.min_client_severity inteiro

Mensagens de biblioteca de clientes muito maiores ou iguais a sybct.min_client_severity serã publicadas como avisos. Este valor também pode ser definido a partir de um script chamando sybase_min_client_severity(). O padrão é 10 que efetivamente desabilita publicações.

sybct.login_timeout inteiro

O máximo de tempo a esperar em segundos por uma tentativa de conexão obter sucesso antes de retornar uma falha. Note que se max_execution_time foi excedido quando uma tentativa de conexão passa do tempo, seu script será terminado antes que possa executar uma ação na falha. O padrão é um minuto.

sybct.timeout inteiro

O máximo de tempo a esperar em segundos por uma select_db ou chamada obter sucesso antes de retornar uma falha. Note que se max_execution_time foi excedido quando uma tentativa de conexão passa do tempo, seu script será terminado antes que possa executar uma ação na falha. O padrão é sem limite.

sybct.hostname string

O nome da máquina a partir da qual você diz se conectar, para ser mostrado por sp_who. O padrão é nenhum (none).


Diretivas de Configuração Informix

ifx.allow_persistent booleano

Permite ou não conexões persistentes Informix.

ifx.max_persistent inteiro

O número máximo de conexões persistentes Informix por processo.

ifx.max_links inteiro

O número máximo de conexões Informix por processo, incluindo conexões persistentes.

ifx.default_host string

A máquina padrão a ser conectada quando nenhuma máquina é especificada em ifx_connect() ou ifx_pconnect().

ifx.default_user string

O id de usuário padrão a ser usado quando nenhum é especificado em ifx_connect() ou ifx_pconnect().

ifx.default_password string

A senha padrão a ser usada quando nenhuma é especificada em ifx_connect() ou ifx_pconnect().

ifx.blobinfile booleano

Defina como TRUE (verdadeiro) se você quer retornar colunas blob num arquivo, falso se você as quer na memória. Você pode sobescrever a definição em tempo de execução com ifx_blobinfile_mode().

ifx.textasvarchar booleano

Defina como TRUE (verdadeiro) se você quer retornar colunas TEXT como strings normais em declarações select, falso se você deseja usar parâmetros id blob. Você pode sobescrever essa definição em tempo de execução com ifx_textasvarchar().

ifx.byteasvarchar booleano

Defina como TRUE (verdadeiro) se você quer retornar colunas BYTE como strings normais em chamadas select, falso se você deseja usar parâmetros id blob. Você pode sobescrever essa definição em tempo de execução com ifx_textasvarchar().

ifx.charasvarchar booleano

Defina como verdadeiro se você deseja cortar espaços de trilha de colunas CHAR ao consultá-las.

ifx.nullformat booleano

Defina como verdadeiro se você quer retornar colunas NULL como a string literal "NULL", falso se você que-las retornadas como uma string vazia "". Você pode sobescrever essa definição em tempo de execução com ifx_nullformat().


Diretivas de Configuração BC Math

bcmath.scale inteiro

Número de dígitos decimais para todas as funções bcmath.


Diretivas de Configuração de Capacidade de Navegador

browscap string

Nome do arquivo de capacidade do navegador. Veja também get_browser().


Diretivas de configuração do Multi-Byte String

mbstring.internal_encoding string

mbstring.internal_encoding define o padrão interno da página de caracteres (character encoding).

mbstring.http_input string

mbstring.http_input define a página de caracteres padrão de entrada HTTP.

mbstring.http_output string

mbstring.http_output define a página de caracteres de saída HTTP.

mbstring.detect_order string

mbstring.detect_order define a ordem de detecção da página de caracteres padrão.

mbstring.substitute_character string

mbstring.substitute_character define a página de caracteres substituta para códigos de páginas inválidas.


Diretivas de configuração Exif

A extensão Exif suporta conversão automática para página de caracteres Unicode e JIS para comentários de usuários quando o a extensão mbstring está disponível. Isto é feito primeiramente decodificando o comentário utilizando o código de página especificada. O resultado é então codificado com o outro código de página para combinar com sua saída HTTP.

exif.encode_unicode string

exif.encode_unicode define a página de código UNICODE nos comentários de usuários manipulados. Seu default é o ISO-8859-15 que funciona para a maioria dos países não-asiáticos. A diretiva pode ser vazia ou precisa ser uma página de código suportada pelo mbstring. Se ela estiver vazia a página de código interna do mbstring é utilizada.

exif.decode_unicode_motorola string

exif.decode_unicode_motorola define o código de página de caracteres Unicode para os comentários de usuários codificados cuja imagem esteja com os bytes na ordem Motorola (big-endian). Esta configuração nao pode ser vazia, mas você pode especificar uma lista de códigos suportados por mbstring. O padrão é UCS-2BE.

exif.decode_unicode_intel string

exif.decode_unicode_intel define o código de página de caracteres Unicode para os comentários de usuários codificados cuja imagem esteja com os bytes na ordem Intel (little-endian). Esta configuração não pode ser vazia, mas você pode especificar uma lista de códigos suportados por mbstring. O padrão é UCS-2LE.

exif.encode_jis string

exif.encode_jis define a página de caracteres para manipulação dos comentários de usuários. Seu default vazio força as funções para usar a página de codificação interna do mbstring.

exif.decode_jis_motorola string

exif.decode_jis_motorola define o código de página de caracteres JIS para os comentários de usuários codificados cuja imagem esteja com os bytes na ordem Motorola (big-endian). Esta configuração não pode ser vazia, mas você pode especificar uma lista de códigos suportados por mbstring. O padrão é JIS.

exif.decode_jis_intel string

exif.decode_jis_intel define o código de página de caracteres JIS para os comentários de usuários codificados cuja imagem esteja com os bytes na ordem Intel (little-endian). esta configuração não pode ser vazia, mas você pode especificar ima lista de códigos suportados por mbstring. O padrão é JIS.


Capítulo 5. Security

PHP is a powerful language and the interpreter, whether included in a web server as a module or executed as a separate CGI binary, is able to access files, execute commands and open network connections on the server. These properties make anything run on a web server insecure by default. PHP is designed specifically to be a more secure language for writing CGI programs than Perl or C, and with correct selection of compile-time and runtime configuration options, and proper coding practices, it can give you exactly the combination of freedom and security you need.

As there are many different ways of utilizing PHP, there are many configuration options controlling its behaviour. A large selection of options guarantees you can use PHP for a lot of purposes, but it also means there are combinations of these options and server configurations that result in an insecure setup.

The configuration flexibility of PHP is equally rivalled by the code flexibility. PHP can be used to build complete server applications, with all the power of a shell user, or it can be used for simple server-side includes with little risk in a tightly controlled environment. How you build that environment, and how secure it is, is largely up to the PHP developer.

This chapter starts with some general security advice, explains the different configuration option combinations and the situations they can be safely used, and describes different considerations in coding for different levels of security.


General considerations

A completely secure system is a virtual impossibility, so an approach often used in the security profession is one of balancing risk and usability. If every variable submitted by a user required two forms of biometric validation (such as a retinal scan and a fingerprint), you would have an extremely high level of accountability. It would also take half an hour to fill out a fairly complex form, which would tend to encourage users to find ways of bypassing the security.

The best security is often inobtrusive enough to suit the requirements without the user being prevented from accomplishing their work, or over-burdening the code author with excessive complexity. Indeed, some security attacks are merely exploits of this kind of overly built security, which tends to erode over time.

A phrase worth remembering: A system is only as good as the weakest link in a chain. If all transactions are heavily logged based on time, location, transaction type, etc. but the user is only verified based on a single cookie, the validity of tying the users to the transaction log is severely weakened.

When testing, keep in mind that you will not be able to test all possibilities for even the simplest of pages. The input you may expect will be completely unrelated to the input given by a disgruntled employee, a cracker with months of time on their hands, or a housecat walking across the keyboard. This is why it's best to look at the code from a logical perspective, to discern where unexpected data can be introduced, and then follow how it is modified, reduced, or amplified.

The Internet is filled with people trying to make a name for themselves by breaking your code, crashing your site, posting inappropriate content, and otherwise making your day interesting. It doesn't matter if you have a small or large site, you are a target by simply being online, by having a server that can be connected to. Many cracking programs do not discern by size, they simply trawl massive IP blocks looking for victims. Try not to become one.


Installed as CGI binary

Possible attacks

Using PHP as a CGI binary is an option for setups that for some reason do not wish to integrate PHP as a module into server software (like Apache), or will use PHP with different kinds of CGI wrappers to create safe chroot and setuid environments for scripts. This setup usually involves installing executable PHP binary to the web server cgi-bin directory. CERT advisory CA-96.11 recommends against placing any interpreters into cgi-bin. Even if the PHP binary can be used as a standalone interpreter, PHP is designed to prevent the attacks this setup makes possible:

  • Accessing system files: http://my.host/cgi-bin/php?/etc/passwd

    The query information in a url after the question mark (?) is passed as command line arguments to the interpreter by the CGI interface. Usually interpreters open and execute the file specified as the first argument on the command line.

    When invoked as a CGI binary, PHP refuses to interpret the command line arguments.

  • Accessing any web document on server: http://my.host/cgi-bin/php/secret/doc.html

    The path information part of the url after the PHP binary name, /secret/doc.html is conventionally used to specify the name of the file to be opened and interpreted by the CGI program. Usually some web server configuration directives (Apache: Action) are used to redirect requests to documents like http://my.host/secret/script.php to the PHP interpreter. With this setup, the web server first checks the access permissions to the directory /secret, and after that creates the redirected request http://my.host/cgi-bin/php/secret/script.php. Unfortunately, if the request is originally given in this form, no access checks are made by web server for file /secret/script.php, but only for the /cgi-bin/php file. This way any user able to access /cgi-bin/php is able to access any protected document on the web server.

    In PHP, compile-time configuration option --enable-force-cgi-redirect and runtime configuration directives doc_root and user_dir can be used to prevent this attack, if the server document tree has any directories with access restrictions. See below for full the explanation of the different combinations.


Case 1: only public files served

If your server does not have any content that is not restricted by password or ip based access control, there is no need for these configuration options. If your web server does not allow you to do redirects, or the server does not have a way to communicate to the PHP binary that the request is a safely redirected request, you can specify the option --enable-force-cgi-redirect to the configure script. You still have to make sure your PHP scripts do not rely on one or another way of calling the script, neither by directly http://my.host/cgi-bin/php/dir/script.php nor by redirection http://my.host/dir/script.php.

Redirection can be configured in Apache by using AddHandler and Action directives (see below).


Case 2: using --enable-force-cgi-redirect

This compile-time option prevents anyone from calling PHP directly with a url like http://my.host/cgi-bin/php/secretdir/script.php. Instead, PHP will only parse in this mode if it has gone through a web server redirect rule.

Usually the redirection in the Apache configuration is done with the following directives:

Action php-script /cgi-bin/php
AddHandler php-script .php

This option has only been tested with the Apache web server, and relies on Apache to set the non-standard CGI environment variable REDIRECT_STATUS on redirected requests. If your web server does not support any way of telling if the request is direct or redirected, you cannot use this option and you must use one of the other ways of running the CGI version documented here.


Case 3: setting doc_root or user_dir

To include active content, like scripts and executables, in the web server document directories is sometimes consider an insecure practice. If, because of some configuration mistake, the scripts are not executed but displayed as regular HTML documents, this may result in leakage of intellectual property or security information like passwords. Therefore many sysadmins will prefer setting up another directory structure for scripts that are accessible only through the PHP CGI, and therefore always interpreted and not displayed as such.

Also if the method for making sure the requests are not redirected, as described in the previous section, is not available, it is necessary to set up a script doc_root that is different from web document root.

You can set the PHP script document root by the configuration directive doc_root in the configuration file, or you can set the environment variable PHP_DOCUMENT_ROOT. If it is set, the CGI version of PHP will always construct the file name to open with this doc_root and the path information in the request, so you can be sure no script is executed outside this directory (except for user_dir below).

Another option usable here is user_dir. When user_dir is unset, only thing controlling the opened file name is doc_root. Opening an url like http://my.host/~user/doc.php does not result in opening a file under users home directory, but a file called ~user/doc.php under doc_root (yes, a directory name starting with a tilde [~]).

If user_dir is set to for example public_php, a request like http://my.host/~user/doc.php will open a file called doc.php under the directory named public_php under the home directory of the user. If the home of the user is /home/user, the file executed is /home/user/public_php/doc.php.

user_dir expansion happens regardless of the doc_root setting, so you can control the document root and user directory access separately.


Case 4: PHP parser outside of web tree

A very secure option is to put the PHP parser binary somewhere outside of the web tree of files. In /usr/local/bin, for example. The only real downside to this option is that you will now have to put a line similar to:

#!/usr/local/bin/php

as the first line of any file containing PHP tags. You will also need to make the file executable. That is, treat it exactly as you would treat any other CGI script written in Perl or sh or any other common scripting language which uses the #! shell-escape mechanism for launching itself.

To get PHP to handle PATH_INFO and PATH_TRANSLATED information correctly with this setup, the php parser should be compiled with the --enable-discard-path configure option.


Installed as an Apache module

When PHP is used as an Apache module it inherits Apache's user permissions (typically those of the "nobody" user). This has several impacts on security and authorization. For example, if you are using PHP to access a database, unless that database has built-in access control, you will have to make the database accessable to the "nobody" user. This means a malicious script could access and modify the database, even without a username and password. It's entirely possible that a web spider could stumble across a database administrator's web page, and drop all of your databases. You can protect against this with Apache authorization, or you can design your own access model using LDAP, .htaccess files, etc. and include that code as part of your PHP scripts.

Often, once security is established to the point where the PHP user (in this case, the apache user) has very little risk attached to it, it is discovered that PHP is now prevented from writing any files to user directories. Or perhaps it has been prevented from accessing or changing databases. It has equally been secured from writing good and bad files, or entering good and bad database transactions.

A frequent security mistake made at this point is to allow apache root permissions, or to escalate apache's abilitites in some other way.

Escalating the Apache user's permissions to root is extremely dangerous and may compromise the entire system, so sudo'ing, chroot'ing, or otherwise running as root should not be considered by those who are not security professionals.

There are some simpler solutions. By using open_basedir you can control and restrict what directories are allowed to be used for PHP. You can also set up apache-only areas, to restrict all web based activity to non-user, or non-system, files.


Filesystem Security

PHP is subject to the security built into most server systems with respect to permissions on a file and directory basis. This allows you to control which files in the filesystem may be read. Care should be taken with any files which are world readable to ensure that they are safe for reading by all users who have access to that filesystem.

Since PHP was designed to allow user level access to the filesystem, it's entirely possible to write a PHP script that will allow you to read system files such as /etc/passwd, modify your ethernet connections, send massive printer jobs out, etc. This has some obvious implications, in that you need to ensure that the files that you read from and write to are the appropriate ones.

Consider the following script, where a user indicates that they'd like to delete a file in their home directory. This assumes a situation where a PHP web interface is regularly used for file management, so the Apache user is allowed to delete files in the user home directories.

Exemplo 5-1. Poor variable checking leads to....

<?php
// remove a file from the user's home directory
$username = $_POST['user_submitted_name'];
$homedir = "/home/$username";
$file_to_delete = "$userfile";
unlink ($homedir/$userfile);
echo "$file_to_delete has been deleted!";
?>
Since the username is postable from a user form, they can submit a username and file belonging to someone else, and delete files. In this case, you'd want to use some other form of authentication. Consider what could happen if the variables submitted were "../etc/" and "passwd". The code would then effectively read:

Exemplo 5-2. ... A filesystem attack

<?php
// removes a file from anywhere on the hard drive that
// the PHP user has access to. If PHP has root access:
$username = "../etc/";
$homedir = "/home/../etc/";
$file_to_delete = "passwd";
unlink ("/home/../etc/passwd");
echo "/home/../etc/passwd has been deleted!";
?>
There are two important measures you should take to prevent these issues.

  • Only allow limited permissions to the PHP web user binary.

  • Check all variables which are submitted.

Here is an improved script:

Exemplo 5-3. More secure file name checking

<?php
// removes a file from the hard drive that
// the PHP user has access to.
$username = $_SERVER['REMOTE_USER']; // using an authentication mechanisim

$homedir = "/home/$username";

$file_to_delete = basename("$userfile"); // strip paths
unlink ($homedir/$file_to_delete);

$fp = fopen("/home/logging/filedelete.log","+a"); //log the deletion
$logstring = "$username $homedir $file_to_delete";
fputs ($fp, $logstring);
fclose($fp);

echo "$file_to_delete has been deleted!";
?>
However, even this is not without it's flaws. If your authentication system allowed users to create their own user logins, and a user chose the login "../etc/", the system is once again exposed. For this reason, you may prefer to write a more customized check:

Exemplo 5-4. More secure file name checking

<?php
$username = $_SERVER['REMOTE_USER']; // using an authentication mechanisim
$homedir = "/home/$username";

if (!ereg('^[^./][^/]*$', $userfile))
     die('bad filename'); //die, do not process

if (!ereg('^[^./][^/]*$', $username))
     die('bad username'); //die, do not process
//etc...
?>

Depending on your operating system, there are a wide variety of files which you should be concerned about, including device entries (/dev/ or COM1), configuration files (/etc/ files and the .ini files), well known file storage areas (/home/, My Documents), etc. For this reason, it's usually easier to create a policy where you forbid everything except for what you explicitly allow.


Database Security

Nowadays, databases are cardinal components of any web based application by enabling websites to provide varying dynamic content. Since very sensitive or secret informations can be stored in such database, you should strongly consider to protect them somehow.

To retrieve or to store any information you need to connect to the database, send a legitimate query, fetch the result, and close the connecion. Nowadays, the commonly used query language in this interaction is the Structured Query Language (SQL). See how an attacker can tamper with an SQL query.

As you can realize, PHP cannot protect your database by itself. The following sections aim to be an introduction into the very basics of how to access and manipulate databases within PHP scripts.

Keep in mind this simple rule: defence in depth. In the more place you take the more action to increase the protection of your database, the less probability of that an attacker succeeds, and exposes or abuse any stored secret information. Good design of the database schema and the application deals with your greatest fears.


Designing Databases

The first step is always to create the database, unless you want to use an existing third party's one. When a database is created, it is assigned to an owner, who executed the creation statement. Usually, only the owner (or a superuser) can do anything with the objects in that database, and in order to allow other users to use it, privileges must be granted.

Applications should never connect to the database as its owner or a superuser, because these users can execute any query at will, for example, modifying the schema (e.g. dropping tables) or deleting its entire content.

You may create different database users for every aspect of your application with very limited rights to database objects. The most required privileges should be granted only, and avoid that the same user can interact with the database in different use cases. This means that if intruders gain access to your database using one of these credentials, they can only effect as many changes as your application can.

You are encouraged not to implement all the business logic in the web application (i.e. your script), instead to do it in the database schema using views, triggers or rules. If the system evolves, new ports will be intended to open to the database, and you have to reimplement the logic in each separate database client. Over and above, triggers can be used to transparently and automatically handle fields, which often provides insight when debugging problems with your application or tracing back transactions.


Connecting to Database

You may want to estabilish the connections over SSL to encrypt client/server communications for increased security, or you can use ssh to encrypt the network connection between clients and the database server. If either of them is done, then monitoring your traffic and gaining informations in this way will be a hard work.


Encrypted Storage Model

SSL/SSH protects data travelling from the client to the server, SSL/SSH does not protect the persistent data stored in a database. SSL is an on-the-wire protocol.

Once an attacker gains access to your database directly (bypassing the webserver), the stored sensitive data may be exposed or misused, unless the information is protected by the database itself. Encrypting the data is a good way to mitigate this threat, but very few databases offer this type of data encryption.

The easiest way to work around this problem is to first create your own encryption package, and then use it from within your PHP scripts. PHP can assist you in this case with its several extensions, such as Mcrypt and Mhash, covering a wide variety of encryption algorithms. The script encrypts the data be stored first, and decrypts it when retrieving. See the references for further examples how encryption works.

In case of truly hidden data, if its raw representation is not needed (i.e. not be displayed), hashing may be also taken into consideration. The well-known example for the hashing is storing the MD5 hash of a password in a database, instead of the password itself. See also crypt() and md5().

Exemplo 5-5. Using hashed password field

// storing password hash
$query  = sprintf("INSERT INTO users(name,pwd) VALUES('%s','%s');",
            addslashes($username), md5($password));
$result = pg_exec($connection, $query);

// querying if user submitted the right password
$query = sprintf("SELECT 1 FROM users WHERE name='%s' AND pwd='%s';",
            addslashes($username), md5($password));
$result = pg_exec($connection, $query);

if (pg_numrows($result) > 0) {
    echo "Welcome, $username!";
}
else {
    echo "Authentication failed for $username.";
}

SQL Injection

Many web developers are unaware of how SQL queries can be tampered with, and assume that an SQL query is a trusted command. It means that SQL queries are able to circumvent access controls, thereby bypassing standard authentication and authorization checks, and sometimes SQL queries even may allow access to host operating system level commands.

Direct SQL Command Injection is a technique where an attacker creates or alters existing SQL commands to expose hidden data, or to override valuable ones, or even to execute dangerous system level commands on the database host. This is accomplished by the application taking user input and combining it with static parameters to build a SQL query. The following examples are based on true stories, unfortunately.

Owing to the lack of input validation and connecting to the database on behalf of a superuser or the one who can create users, the attacker may create a superuser in your database.

Exemplo 5-6. Splitting the result set into pages ... and making superusers (PostgreSQL and MySQL)

$offset = argv[0]; // beware, no input validation!
$query  = "SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET $offset;";
// with PostgreSQL 
$result = pg_exec($conn, $query);
// with MySQL
$result = mysql_query($query);
Normal users click on the 'next', 'prev' links where the $offset is encoded into the URL. The script expects that the incoming $offset is decimal number. However, someone tries to break in with appending urlencode()'d form of the following to the URL

// in case of PostgreSQL
0;
insert into pg_shadow(usename,usesysid,usesuper,usecatupd,passwd)
    select 'crack', usesysid, 't','t','crack'
    from pg_shadow where usename='postgres';
--

// in case of MySQL
0;
UPDATE user SET Password=PASSWORD('crack') WHERE user='root';
FLUSH PRIVILEGES;

If it happened, then the script would present a superuser access to him. Note that 0; is to supply a valid offset to the original query and to terminate it.

Nota: It is common technique to force the SQL parser to ignore the rest of the query written by the developer with -- which is the comment sign in SQL.

A feasible way to gain passwords is to circumvent your search result pages. What the attacker needs only is to try if there is any submitted variable used in SQL statement which is not handled properly. These filters can be set commonly in a preceding form to customize WHERE, ORDER BY, LIMIT and OFFSET clauses in SELECT statements. If your database supports the UNION construct, the attacker may try to append an entire query to the original one to list passwords from an arbitrary table. Using encrypted password fields is strongly encouraged.

Exemplo 5-7. Listing out articles ... and some passwords (any database server)

$query  = "SELECT id, name, inserted, size FROM products
                  WHERE size = '$size'
                  ORDER BY $order LIMIT $limit, $offset;";
$result = odbc_exec($conn, $query);
The static part of the query can be combined with another SELECT statement which reveals all passwords:

'
union select '1', concat(uname||'-'||passwd) as name, '1971-01-01', '0' from usertable;
--

If this query (playing with the ' and --) were assigned to one of the variables used in $query, the query beast awakened.

SQL UPDATEs are also subject to attacking your database. These queries are also threatened by chopping and appending an entirely new query to it. But the attacker might fiddle with the SET clause. In this case some schema information must be possessed to manipulate the query successfully. This can be acquired by examing the form variable names, or just simply brute forcing. There are not so many naming convention for fields storing passwords or usernames.

Exemplo 5-8. From resetting a password ... to gaining more privileges (any database server)

$query = "UPDATE usertable SET pwd='$pwd' WHERE uid='$uid';";
But a malicious user sumbits the value ' or uid like'%admin%'; -- to $uid to change the admin's password, or simply sets $pwd to "hehehe', admin='yes', trusted=100 " (with a trailing space) to gain more privileges. Then, the query will be twisted:

// $uid == ' or uid like'%admin%'; --
$query = "UPDATE usertable SET pwd='...' WHERE uid='' or uid like '%admin%'; --";

// $pwd == "hehehe', admin='yes', trusted=100 "
$query = "UPDATE usertable SET pwd='hehehe', admin='yes', trusted=100 WHERE ...;"

A frightening example how operating system level commands can be accessed on some database hosts.

Exemplo 5-9. Attacking the database host's operating system (MSSQL Server)

$query  = "SELECT * FROM products WHERE id LIKE '%$prod%'";
$result = mssql_query($query);
If attacker submits the value a%' exec master..xp_cmdshell 'net user test testpass /ADD' -- to $prod, then the $query will be:

$query  = "SELECT * FROM products
                    WHERE id LIKE '%a%'
                    exec master..xp_cmdshell 'net user test testpass /ADD'--";
$result = mssql_query($query);

MSSQL Server executes the SQL statements in the batch including a command to add a new user to the local accounts database. If this application were running as sa and the MSSQLSERVER service is running with sufficient privileges, the attacker would now have an account with which to access this machine.

Nota: Some of the examples above is tied to a specific database server. This does not mean that a similar attack is impossible against other products. Your database server may be so vulnerable in other manner.


Avoiding techniques

You may plead that the attacker must possess a piece of information about the database schema in most examples. You are right, but you never know when and how it can be taken out, and if it happens, your database may be exposed. If you are using an open source, or publicly available database handling package, which may belong to a content management system or forum, the intruders easily produce a copy of a piece of your code. It may be also a security risk if it is a poorly designed one.

These attacks are mainly based on exploiting the code not being written with security in mind. Never trust on any kind of input, especially which comes from the client side, even though it comes from a select box, a hidden input field or a cookie. The first example shows that such a blameless query can cause disasters.

  • Never connect to the database as a superuser or as the database owner. Use always customized users with very limited privileges.

  • Check if the given input has the expected data type. PHP has a wide range of input validating functions, from the simplest ones found in Variable Functions and in Character Type Functions (e.g. is_numeric(), ctype_digit() respectively) onwards the Perl compatible Regular Expressions support.

  • If the application waits for numerical input, consider to verify data with is_numeric(), or silently change its type using settype(), or use its numeric representation by sprintf().

    Exemplo 5-10. A more secure way to compose a query for paging

    settype($offset, 'integer');
    $query = "SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET $offset;";
    
    // please note %d in the format string, using %s would be meaningless
    $query = sprintf("SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET %d;",
                     $offset);

  • Quote each non numeric user input which is passed to the database with addslashes() or addcslashes(). See the first example. As the examples shows, quotes burnt into the static part of the query is not enough, and can be easily hacked.

  • Do not print out any database specific information, especially about the schema, by fair means or foul. See also Error Reporting and Error Handling and Logging Functions.

  • You may use stored procedures and previously defined cursors to abstract data access so that users do not directly access tables or views, but this solution has another impacts.

Besides these, you benefit from logging queries either within your script or by the database itself, if it supports. Obviously, the logging is unable to prevent any harmful attempt, but it can be helpful to trace back which application has been circumvented. The log is not useful by itself, but through the information it contains. The more detail is generally better.


Error Reporting

With PHP security, there are two sides to error reporting. One is beneficial to increasing security, the other is detrimental.

A standard attack tactic involves profiling a system by feeding it improper data, and checking for the kinds, and contexts, of the errors which are returned. This allows the system cracker to probe for information about the server, to determine possible weaknesses. For example, if an attacker had gleaned information about a page based on a prior form submission, they may attempt to override variables, or modify them:

Exemplo 5-11. Attacking Variables with a custom HTML page

<form method="post" action="attacktarget?username=badfoo&password=badfoo">
<input type="hidden" name="username" value="badfoo">
<input type="hidden" name="password" value="badfoo">
</form>

The PHP errors which are normally returned can be quite helpful to a developer who is trying to debug a script, indicating such things as the function or file that failed, the PHP file it failed in, and the line number which the failure occured in. This is all information that can be exploited. It is not uncommon for a php developer to use show_source(), highlight_string(), or highlight_file() as a debugging measure, but in a live site, this can expose hidden variables, unchecked syntax, and other dangerous information. Especially dangerous is running code from known sources with built-in debugging handlers, or using common debugging techniques. If the attacker can determine what general technique you are using, they may try to brute-force a page, by sending various common debugging strings:

Exemplo 5-12. Exploiting common debugging variables

<form method="post" action="attacktarget?errors=Y&amp;showerrors=1"&debug=1">
<input type="hidden" name="errors" value="Y">
<input type="hidden" name="showerrors" value="1">
<input type="hidden" name="debug" value="1">
</form>

Regardless of the method of error handling, the ability to probe a system for errors leads to providing an attacker with more information.

For example, the very style of a generic PHP error indicates a system is running PHP. If the attacker was looking at an .html page, and wanted to probe for the back-end (to look for known weaknesses in the system), by feeding it the wrong data they may be able to determine that a system was built with PHP.

A function error can indicate whether a system may be running a specific database engine, or give clues as to how a web page or programmed or designed. This allows for deeper investigation into open database ports, or to look for specific bugs or weaknesses in a web page. By feeding different pieces of bad data, for example, an attacker can determine the order of authentication in a script, (from the line number errors) as well as probe for exploits that may be exploited in different locations in the script.

A filesystem or general PHP error can indicate what permissions the webserver has, as well as the structure and organization of files on the web server. Developer written error code can aggravate this problem, leading to easy exploitation of formerly "hidden" information.

There are three major solutions to this issue. The first is to scrutinize all functions, and attempt to compensate for the bulk of the errors. The second is to disable error reporting entirely on the running code. The third is to use PHP's custom error handling functions to create your own error handler. Depending on your security policy, you may find all three to be applicable to your situation.

One way of catching this issue ahead of time is to make use of PHP's own error_reporting(), to help you secure your code and find variable usage that may be dangerous. By testing your code, prior to deployment, with E_ALL, you can quickly find areas where your variables may be open to poisoning or modification in other ways. Once you are ready for deployment, by using E_NONE, you insulate your code from probing.

Exemplo 5-13. Finding dangerous variables with E_ALL

<?php
if ($username) {  // Not initialized or checked before usage
    $good_login = 1;
}
if ($good_login == 1) { // If above test fails, not initialized or checked before usage
    fpassthru ("/highly/sensitive/data/index.html");
}
?>


Using Register Globals

One feature of PHP that can be used to enhance security is configuring PHP with register_globals = off. By turning off the ability for any user-submitted variable to be injected into PHP code, you can reduce the amount of variable poisoning a potential attacker may inflict. They will have to take the additional time to forge submissions, and your internal variables are effectively isolated from user submitted data.

While it does slightly increase the amount of effort required to work with PHP, it has been argued that the benefits far outweigh the effort.

Exemplo 5-14. Working without register_globals=off

<?php
if ($username) {  // can be forged by a user in get/post/cookies
    $good_login = 1;
}

if ($good_login == 1) { // can be forged by a user in get/post/cookies,
    fpassthru ("/highly/sensitive/data/index.html");
}
?>

Exemplo 5-15. Working with register_globals = off

<?php
if($_COOKIE['username']){
    // can only come from a cookie, forged or otherwise
    $good_login = 1;
    fpassthru ("/highly/sensitive/data/index.html");
}
?>
By using this wisely, it's even possible to take preventative measures to warn when forging is being attempted. If you know ahead of time exactly where a variable should be coming from, you can check to see if submitted data is coming from an inappropriate kind of submission. While it doesn't guarantee that data has not been forged, it does require an attacker to guess the right kind of forging.

Exemplo 5-16. Detecting simple variable poisoning

<?php
if ($_COOKIE['username'] &&
    !$_POST['username'] &&
    !$_GET['username'] ) {
    // Perform other checks to validate the user name...
    $good_login = 1;
    fpassthru ("/highly/sensitive/data/index.html");
} else {
   mail("admin@example.com", "Possible breakin attempt", $_SERVER['REMOTE_ADDR']);
   echo "Security violation, admin has been alerted.";
   exit;
}
?>
Of course, simply turning off register_globals does not mean code is secure. For every piece of data that is submitted, it should also be checked in other ways.


User Submitted Data

The greatest weakness in many PHP programs is not inherent in the language itself, but merely an issue of code not being written with security in mind. For this reason, you should always take the time to consider the implications of a given piece of code, to ascertain the possible damage if an unexpected variable is submitted to it.

Exemplo 5-17. Dangerous Variable Usage

<?php
// remove a file from the user's home directory... or maybe
// somebody else's?
unlink ($evil_var);

// Write logging of their access... or maybe an /etc/passwd entry?
fputs ($fp, $evil_var);

// Execute something trivial.. or rm -rf *?
system ($evil_var);
exec ($evil_var);

?>
You should always carefully examine your code to make sure that any variables being submitted from a web browser are being properly checked, and ask yourself the following questions:

  • Will this script only affect the intended files?

  • Can unusual or undesirable data be acted upon?

  • Can this script be used in unintended ways?

  • Can this be used in conjunction with other scripts in a negative manner?

  • Will any transactions be adequately logged?

By adequately asking these questions while writing the script, rather than later, you prevent an unfortunate re-write when you need to increase your security. By starting out with this mindset, you won't guarantee the security of your system, but you can help improve it.

You may also want to consider turning off register_globals, magic_quotes, or other convenience settings which may confuse you as to the validity, source, or value of a given variable. Working with PHP in error_reporting(E_ALL) mode can also help warn you about variables being used before they are checked or initialized (so you can prevent unusual data from being operated upon).


Hiding PHP

In general, security by obscurity is one of the weakest forms of security. But in some cases, every little bit of extra security is desirable.

A few simple techniques can help to hide PHP, possibly slowing down an attacker who is attempting to discover weaknesses in your system. By setting expose_php = off in your php.ini file, you reduce the amount of information available to them.

Another tactic is to configure web servers such as apache to parse different filetypes through PHP, either with an .htaccess directive, or in the apache configuration file itself. You can then use misleading file extensions:

Exemplo 5-18. Hiding PHP as another language

# Make PHP code look like other code types
AddType application/x-httpd-php .asp .py .pl
Or obscure it completely:

Exemplo 5-19. Using unknown types for PHP extensions

# Make PHP code look like unknown types
AddType application/x-httpd-php .bop .foo .133t
Or hide it as html code, which has a slight performance hit because all html will be parsed through the PHP engine:

Exemplo 5-20. Using html types for PHP extensions

# Make all PHP code look like html
AddType application/x-httpd-php .htm .html
For this to work effectively, you must rename your PHP files with the above extensions. While it is a form of security through obscurity, it's a minor preventative measure with few drawbacks.


Keeping Current

PHP, like any other large system, is under constant scrutiny and improvement. Each new version will often include both major and minor changes to enhance and repair security flaws, configuration mishaps, and other issues that will affect the overall security and stability of your system.

Like other system-level scripting languages and programs, the best approach is to update often, and maintain awareness of the latest versions and their changes.


Capítulo 6. Sintaxe básica

Alternado/Escaping do HTML

Quando o PHP interpreta um arquivo, ele simplesmente repassa o texto do arquivo até encontrar uma das tags especiais que lhe diz para começar a interpretar o texto como código PHP. O interpretador então executa todo o código que encontra, até chegar em uma tag de fechamento PHP, que novamente o coloca simplesmente repassando texto novamente. Este é o mecanismo que permite a inclusão de código PHP dentro do HTML: qualquer coisa fora das tags PHP é deixado como encontrado, enquanto tudo dentro é interpretado e executado.

Há quatro conjuntos de tags que podem ser usadas para marcar blocos de código PHP. Delas, somente duas (<?php. . .?> e <script language="php">. . .</script>) são sempre disponíveis. As outras podem ser ativadas ou desativadas a partir do arquivo de configuração php.ini. Enquanto as formas reduzidas das tags ou no seu estilo ASP serem convenientes, elas não são portáveis em todas as versões. Além disso, se você pretende incluir código PHP em XML ou XHTML, você precisará usar a forma <?php ... ?> para compatibilidade com o padrão XML.

As tags suportadas pelo PHP são:

Exemplo 6-1. Maneiras de alternar do HTML

1.  <?php echo("se você precisa dispor documentos XHTML ou XML, use assim\n"); ?>

2.  <? echo ("este é o mais simples, como uma instrução de processamento SGML\n"); ?>
    <?= espressao ?> Uma redução de "<? echo expressao ?>"

3.  <script language="php">
        echo ("alguns editores (como o FrontPage) não
              gostam de processas instruções");
    </script>

4.  <% echo ("Você também pode usar tags ASP opcionalmente"); %>
    <%= $variavel; # Uma redução para "<% echo ..." %>

O primeiro método, <?php. . .?>, é o preferencial, já que ele permite o uso do PHP em códigos padrão XML como o XHTML.

O segundo método pode não estar sempre disponível. Tags curtas estão disponíveis apenas quando ativadas. Isto pode ser realizando através da função short_tags() (PHP 3 somente), ativando a diretiva de configuração short_open_tag no arquivo de configuração do PHP ou compilando o PHP com a opção --enable-short-tags no configure. Mesmo que ele esteja configurado por default no php.ini-dist, o uso de tags curtas é desencorajado.

A quarta maneira só está disponível se a tag estilo ASP for ativada utilizando a diretiva asp_tags no arquivo de configuração.

Nota: O suporte as tags estilo APS foi incorporada na versão 3.0.4.

Nota: A utilização das tags curtas deve ser evitada quando do desenvolvimento de aplicações ou bibliotecas com intenção de redistribuição ou no desenvolvimento de serviços em PHP que não ficarão sob seu controle, uma vez que as tags curtas podem não estar disponíveis no servidor de instalação. Para portabilidade de código para distribuição, tenha certeza de não usar tags curtas.

A tag de fechamento incluirá uma linha nova linha em branco automaticamente se uma não estiver presente. Além, a tag de fechamento automaticamente implica num ponto e vírgula: você não precisa ter um ponto e vírgula no fim da última linha de código PHP.

O PHP também suporta a utilização de estruturas como essa:

Exemplo 6-2. Alternagem avançada

<?php
if ($expression) {
    ?>
    <strong>Isso é verdadeiro.</strong>
    <?php
} else {
    ?>
    <strong>Isto é falso.</strong>
    <?php
}
?>
Isso funciona como esperado porque quando o PHP encontra a tag de fechamento ?>, ele simplesmente começa a imprimir tudo até encontrar outra tag de abertura. Obviamente, o exemplo acima se aplica a exibição de grandes blocos de texto, uma vez que sair do modo de interpretação do PHP é geralmente mais eficiente que imprimir todo o texto através de funções como echo(), print() e outras.


Separador de instruções

Instruções são separadas da mesma forma que o C ou o Perl - cada instrução termina com um ponto e vírgula.

A tag de fechamento (?>) também implica no fim de uma instrução, então os exemplos seguintes são equivalentes:

<?php
    echo "Isto é um teste";
?>

<?php echo "Isto é um outro teste" ?>


Comentários

O PHP suporta comentários do 'C', 'C++' e Unix shell. Por exemplo

<?php
    echo "Isto é um teste"; //Comentário de uma linha no C++
    /* Isto é um comentário de mais de uma linha
       e aqui temos outra linha */
    echo "Isto é um outro teste";
    echo "O último teste"; #Comentário no estilo Unix shell
?>

Os comentário de uma linha só tem efeito até o fim da linha ou fim do bloco de código PHP atual, o que ocorrer primeiro.

<h1>Isto é um <?php # echo " simples";?> exemplo.</h1>
<p>No título acima você lerá 'Isto é um exemplo'.

Você precisa ser cuidadoso com comentários estilo 'C' encadeados, pois eles podem causar problemas em grandes blocos.

<?php
 /*
    echo "Isto é um teste"; /* Este comentário causará um erro */
 */
?>

Os comentários de uma linha somente agem até o fim da linha atual ou o fim do bloco de código PHP, o que ocorrer primeiro. Isto significa que código HTML após // ?> SERÁ impresso: ?> continuará desligando o modo PHP, retornando para o modo HTML, e o // não pode influenciar isso.


Capítulo 7. Tipos

O PHP suporta os seguintes tipos:

O tipo da variável geralmente não é setado pelo programador; em vez disso, é decidido em tempo de execução pelo PHP, dependendo do contexto no qual a variável é usada.

Se você gostaria de forçar uma variável a ser convertida para um certo tipo, você pode moldar a variável ou usar a função settype() nela.

Note que uma variável pode se comportar de maneiras diferentes em certas situações, dependendo de qual tipo ela é no momento. Para mais informações, veja a seção Manipulação de tipos.


Inteiros

Inteiros podem ser especificados usando-se qualquer uma das seguintes sintaxes:

$a = 1234; # número decimal 
$a = -123; # um número negativo
$a = 0123; # número octal (equivalente a 83 decimal)
$a = 0x12; # número hexadecimal (equivalente a 18 decimal)


Números de ponto flutuante

Números de ponto flutuante ("doubles") podem ser especificados usando-se qualquer uma das seguintes sintaxes:

$a = 1.234; $a = 1.2e3;


Strings

Strings podem ser especificadas usando-se um dos dois conjuntos de delimitadores.

Se a string está englobada entre aspas ("), as variáveis dentro da string serão expandidas (sujeitas a algumas limitações de análise). Como em C e em Perl, a barra invertida ("\") pode ser usada para especificar caracteres especiais:

Tabela 7-1. Caracteres de escape

sequencemeaning
\nnova linha
\rretorno de carro
\ttabulação horizontal
\\barra invertida
\$cifrão
\"aspas
\[0-7]{1,3} a sequência de caracteres que casa com a expressão regular é um caractere na notação octal
\x[0-9A-Fa-f]{1,2} a sequência de caracteres que casa com a expressão regular é um caractere na notação hexadecimal

Você pode escapar qualquer outro caractere, mas um alerta será emitido no nível mais alto de alertas.

A segunda maneira de delimitar uma string usa o apóstrofe ("'"). Quando uma string é englobada por apóstrofes, os únicos escapes que serão entendidos são "\\" e "\'". Isto é por conveniência, de forma que você tenha apóstrofes e barras invertidas em uma string com apóstrofes. As variáveis não serão expandidas dentro de uma string com apóstrofes.

Outra maneira de delimitar strings é pelo uso da sintaxe 'here doc' ("<<<"). Alguém deve fornecer um identificador depois de <<<, depois a string, e então o mesmo identificador para fechar a citação. O identificador de fechamento precisa começar na primeira coluna da linha.

Exemplo 7-1. Exemplo de citação de string 'here doc'

$str = <<<EOD
Exemplo de string
dividida em múltiplas linhas 
usando a sintaxe 'heredoc'.
EOD;

Nota: Suporte a 'here doc' foi acrescentado no PHP 4.

Strings podem ser concatenadas usando-se o operador '.' (ponto). Note que o operador '+' (adição) não funcionará para isso. Por favor veja Operadores de string para mais informações.

Caracteres dentro de strings podem ser acessados pelo tratamento da string como uma matriz de caracteres indexadas numericamente, usando a sintaxe no estilo C. Veja alguns exemplos abaixo.

Exemplo 7-2. Alguns exemplos de string

<?php
/* Atribuindo uma string. */
$str = "Isto é uma string";

/* Acrescentando a ela. */
$str = $str . " com mais um pouco de texto";

/* Outra maneira de acrescentar, inclui uma nova linha com escape. */
$str .= " e uma nova linha no fim.\n";

/* Esta string terminará sendo '<p>Número: 9</p>' */
$num = 9;
$str = "<p>Número: $num</p>";

/* Esta será '<p>Número: $num</p>' */
$num = 9;
$str = '<p>Number: $num</p>';

/* Obtém o primeiro caractere da string  */
$str = 'Isto é um teste.';
$first = $str[0];

/* Obtém o último caractere de uma string. */
$str = 'Isto ainda é um teste.';
$last = $str[strlen($str)-1];
?>


Conversão de String

Quando uma string é avaliada como um valor numérico, o valor resultante e o tipo são determinados como se segue.

A string será avaliada como um 'double' se contiver qualquer um dos caracteres '.', 'e', ou 'E'. Caso contrário, será avaliada como um inteiro.

O valor é dado pela porção inicial da string. Se a string começa com dados numéricos válidos, estes serão o valor usado. Caso contrário, o valor será 0 (zero). Dados numéricos válidos são um sinal opcional, seguido por um ou mais dígitos (opcionalmente contendo um ponto decimal), seguido por um expoente opcional. O expoente é um 'e' ou 'E' seguido por um ou mais dígitos.

Quando a primeira expressão é uma string, o tipo de variável dependerá da segunda expressão.

$foo = 1 + "10.5";              // $foo é 'double' (11.5)
$foo = 1 + "-1.3e3";            // $foo é 'double' (-1299)
$foo = 1 + "bob-1.3e3";         // $foo é inteiro (1)
$foo = 1 + "bob3";              // $foo é inteiro (1)
$foo = 1 + "10 porquinhos";     // $foo é inteiro (11)
$foo = 1 + "10 porquinhos";     // $foo é inteiro (11)
$foo = "10.0 porcos " + 1;      // $foo é inteiro (11)
$foo = "10.0 porcos " + 1.0;    // $foo é 'double' (11)

Para mais informações sobre esta conversão, veja a página de manual de strtod(3).

Se você gostaria de testar qualquer um dos exemplos desta seção, você pode cortar e colar os exemplos e inserir a seguinte linha para ver, por você mesmo, o que está acontecendo:

echo "o tipo de \$foo==$foo; é " . gettype ($foo) . "<br>\n";


Matrizes

Matrizes agem atualmente como tabelas de 'hashing' (matrizes associativas) e matrizes indexadas (vetores).


Matrizes de Uma Dimensão

O PHP suporta matrizes escalares e associativas. De fato, não há diferença entre as duas. Você pode criar uma matriz usando as funções list() ou array(), ou você pode setar cada valor dos elementos da matriz explicitamente.

$a[0] = "abc"; 
$a[1] = "def"; 
$b["foo"] = 13;

Você também pode criar uma matriz simplesmente acrescentando valores à matriz. Quando você atribui um valor a uma variável de matriz usando colchetes vazios, o valor será acrescentado ao final da matriz.

$a[] = "olá"; // $a[2] == "olá"
$a[] = "mundo"; // $a[3] == "mundo"

As matrizes podem ser ordenadas usando-se as funções asort(), arsort(), ksort(), rsort(), sort(), uasort(), usort(), e uksort(), dependendo do tipo de ordenação que você quer.

Você pode contar o número de itens em uma matriz usando a função count().

Você pode atravessar uma matriz usando as funções next() e prev(). Uma outra maneira comum de atravessar uma matriz é com o uso da função each().


Matrizes Multi-Dimensionais

Matrizes multi-dimensionais são bem simples atualmente. Para cada dimensão da matriz, você acrescenta outro valor [chave] no final:

$a[1]      = $f;               # exemplos de uma dimensão
$a["foo"]  = $f;   

$a[1][0]     = $f;             # duas dimensões
$a["foo"][2] = $f;             # (você pode misturar índices numéricos e associativos)
$a[3]["bar"] = $f;             # (você pode misturar índices numéricos e associativos)

$a["foo"][4]["bar"][0] = $f;   # quatro dimensões!

Em PHP3 não é possível referenciar matrizes multidimensionais diretamente dentro de strings. Por exemplo, o que vem a seguir não terá o resultado desejado:

$a[3]['bar'] = 'Bob';
echo "Isto não vai funcionar: $a[3][bar]";

Em PHP3, o impresso acima será Isto não vai funcionar: Array[bar]. O operador de concatenação de string, porém, pode ser usado para sobrepôr isto:

$a[3]['bar'] = 'Bob';
echo "Isto não vai funcionar: " . $a[3][bar];

Em PHP4, porém, o problema todo pode ser acobertado englobando a referência da matriz (dentro da string) entre chaves:

$a[3]['bar'] = 'Bob';
echo "Isto vai funcionar: {$a[3][bar]}";

Você pode "preencher" matrizes multidimensionais de muitas formas, mas a mais fácil de entender é como usar o comando array() para matrizes associativas. Estes dois trechos de código preenchem a matriz unidimensional da mesma forma:

# Exemplo 1:

$a["cor"]	= "vermelho";
$a["sabor"]	= "doce";
$a["forma"]	= "redondo";
$a["nome"]	= "maçã";
$a[3]		= 4;

# Exemplo 2:
$a = array(
     "cor" => "vermelho",
     "sabor" => "doce",
     "forma" => "redondo",
     "nome"  => "maçã",
     3       => 4
);

A função array() pode ser aninhada para matrizes multidimensionais:

<?
$a = array(
     "maçã"  => array(
          "cor"  => "vermelho",
          "sabor"  => "doce",
          "forma"  => "redondo"
     ),
     "laranja"  => array(
          "cor"  => "laranja",
          "sabor"  => "azedo",
          "forma"  => "redondo"
     ),
     "banana"  => array(
          "cor"  => "amarelo",
          "sabor"  => "pastoso",
          "forma"  => "cilíndrico"
     )
);

echo $a["maçã"]["sabor"];    # imprimirá "doce"
?>


Objetos

Inicialização de Objeto

Para inicializar um objeto, você usa o comando new para instanciar o objeto para uma variável.

<?php
class foo {
    function do_foo() { 
        echo "Fazendo foo."; 
    }
}

$bar = new foo;
$bar->do_foo();
?>

Para uma discussão completa, por favor leia a seção Classes e Objetos.


Type Juggling

O PHP não requer (ou suporta) a definição explícita de tipo na declaração da variável; um tipo de variável é determinado pelo contexto na qual a variável é usada. Quer dizer, se você atribui um valor de string a uma variável var, var se torna uma string. Se você então atribui um valor inteiro a var, ela se torna inteira.

Um exemplo de conversão automática de tipo no PHP é o operador de adição '+'. Se qualquer um dos operandos é um 'double', então todos os operandos são avaliados como 'doubles', e o resultado será um 'double'. Caso contrário, os operandos serão interpretados como inteiros, e o resultado também será um inteiro. Note que isto NÃO muda os tipos dos próprios operandos; a única mudança é em como os operandos são avaliados.

$foo = "0";  // $foo é string (ASCII 48)
$foo++;      // $foo é a string "1" (ASCII 49)
$foo += 1;   // $foo agora é um inteiro (2)
$foo = $foo + 1.3;  // $foo agora é um double (3.3)
$foo = 5 + "10 Porquinhos"; // $foo é inteiro (15)
$foo = 5 + "10 Porquinhos";     // $foo é inteiro (15)

Se os últimos dois exemplos acima parecem incomuns, veja Conversão de Strings.

Se você deseja forçar uma variável a ser avaliada como um certo tipo, veja a seção sobreMoldagem de tipo. Se você deseja mudar o tipo de uma variável, veja settype().

Se você gostaria de testar qualquer um dos exemplos desta seção, você pode cortar e colar os exemplos e inserir a seguinte linha para ver, por você mesmo, o que está acontecendo:

echo "o tipo de \$foo==$foo; é " . gettype ($foo) . "<br>\n";

Nota: O comportamento de uma conversão automática para matriz não é definida no momento.

$a = 1;       // $a é um inteiro
$a[0] = "f";  // $a se torna uma matriz, com $a[0] mantendo "f"

Enquanto o exemplo acima pode parecer que vai claramente resultar em $a se tornar uma matriz, do qual o primeiro elemento é 'f', considere isto:

$a = "1";     // $a é uma string
$a[0] = "f";  // E as compensações de string? O que acontece?

Como o PHP suporta indexação para strings através de compensações usando a mesma sintaxe que a indexação de matrizes, o exemplo acima conduz a um problema: $a deveria se tornar uma matriz com seu primeiro elemento sendo "f", ou "f" deveria se tornar o primeiro caractere da string $a?

Por esta razão, como no PHP 3.0.12 e no PHP 4.0b3-RC4, o resultado desta conversão automática é considerado indefinido. Correções estão, portanto, sendo discutidas.


Moldagem de tipo

Moldagem de tipo no PHP funciona de forma semelhante ao C: o nome do tipo desejado é escrito entre parênteses antes da variável que se quer moldar.

$foo = 10;   // $foo é um inteiro
$bar = (double) $foo;   // $bar é um double

As moldagens permitidas são:

  • (int), (integer) - molda para inteiro

  • (real), (double), (float) - molda para 'double'

  • (string) - molda para string

  • (array) - molda para matriz

  • (object) - molda para objeto

Note que tabulações e espaços são permitidos dentro dos parênteses, assim os exemplos a seguir são funcionalmente equivalentes:

$foo = (int) $bar;
$foo = ( int ) $bar;

Pode não ser exatamente óbvio o que acontece quando ocorre a moldagem entre certos tipos. Por exemplo, o seguinte deve ser notado.

Quando se molda uma variável escalar ou string para uma matriz, a variável se tornará o primeiro elemento da matriz:

$var = 'tchau';
$arr = (array) $var;
echo $arr[0];  // imprime 'tchau'

Quando se molda uma variável escalar ou string para um objeto, a variável se tornará um atributo do objeto; o nome do atributo será 'scalar':

$var = 'tchau';
$obj = (object) $var;
echo $obj->scalar;  // imprime 'ciao'


Capítulo 8. Variáveis

Introdução

As variáveis no PHP são representadas por um cifrão ($) seguido pelo nome da variável. Os nomes de variável no PHP fazem distinção entre maiúsculas e minúsculas.

Os nomes de variável seguem as mesmas regras como outros rótulos no PHP. Um nome de variável válido se inicia com uma letra ou sublinhado, seguido de qualquer número de letras, algarismos ou sublinhados. Em uma expressão regular isto poderia ser representado desta forma: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

Nota: Para nossos propósitos, as letras a-z, A-Z e os caracteres ASCII de 127 a 255 (0x7f-0xff).

<?php
$var = "Bob";
$Var = "Joe";
echo "$var, $Var";      // exibe "Bob, Joe"

$4site = 'not yet';     // inválido; começa com um número
$_4site = 'not yet';    // válido; começa com um sublinhado
$täyte = 'mansikka';    // válido; 'ä' é um caracter ASCII 228.
?>

No PHP 3, as variáveis são sempre atribuídas por valor. Isto significa que quando você atribui uma expressão a uma variável, o valor da expressão original é copiado integralmente para a variável de destino. Isto significa também que, após atribuir o valor de uma variável a outra, a alteração de uma destas variáveis não afetará a outra. Para maiores informações sobre este tipo de atribuição, veja o capítulo em Expressões.

O PHP 4 oferece um outro meio de atribuir valores a variáveis: a atribuição por referência. Isto significa que a nova variável simplesmente referencia (em outras palavras, "torna-se um apelido para" ou "aponta para") a variável original. Alterações na nova variável afetam a original e vice versa. Isto significa também que nenhuma cópia é realizada, de modo que a atribuição ocorre mais rapidamente. Entretanto, qualquer aumento de velocidade só será realmente notado em *loops* complexos ou em atribuições de grandes arrays ou objetos.

Para atribuir por referência, simplesmente adicione um e-comercial (&) na frente do nome da variável que estiver sendo atribuída (variável de origem) Por exemplo, o trecho de código abaixo imprime 'My name is Bob' duas vezes:

<?php
$foo = 'Bob';              // Atribui o valor 'Bob' a variável $foo
$bar = &$foo;              // Referecia $foo através de $bar.
$bar = "My name is $bar";  // Altera $bar...
echo $bar;
echo $foo;                 // $foo é alterada também.
?>

Uma observação importante a se fazer: somente variáveis nomeadas podem ser atribuídas por referência.

<?php
$foo = 25;
$bar = &$foo;      // Esta atribuição é válida.
$bar = &(24 * 7);  // Inválido; referencia uma expressão sem nome.

function test()
{
   return 25;
}

$bar = &test();    // Inválido.
?>


Variáveis Predefinidas

O PHP oferece um grande número de variáveis predefinidas para qualquer script que ele execute. Muitas destas variáveis, entretanto, não podem ser completamente documentadas uma vez dependem de diversos fatores, como o servidor no qual scripts são executados, a versão e configuração deste servidor e outros. Algumas destas variáveis não estarão disponíveis quando o PHP for executado na linha de comando. Para uma lista destas variáveis, veja a seção Variáveis reservadas.

Atenção

No PHP 4.2.0 e posteriores, o valor default da diretiva register_globals é off. Esta é a maior modificação no PHP. Tendo register_globals off afeta o conjunto de variáveis predefinidas disponíveis no escopo global. POr exemplo, para ler DOCUMENT_ROOT você usará $_SERVER['DOCUMENT_ROOT'] em vez de $DOCUMENT_ROOT, ou $_GET['id'] da URL http://www.example.com/test.php?id=3 em vez de $id, or $_ENV['HOME'] em vez de $HOME.

Para informações relacionadas desta modificação, veja detalhes da diretiva register_globals, no capítulo de segurança em Usando register_globals , assim como o detalhamento de lançamento das versões do PHP 4.1.0 e 4.2.0.

Utilizar as Variáveis Predefinidas do PHP, como os arrays superglobais, é muito mais preferível.

Desde a versão 4.1.0, o PHP fornece um conjunto adicional de arrays predefinidos contendo as variáveis do servidor web (se aplicável), as variáveis ambiente e as entradas do usuário. Esses novos arrays são especiais pelo motivo que são automaticamente globais (significa que são automaticamente disponíveis em qualquer escopo. Por causa disso, são também conhecidas como 'autoglobais' ou 'superglobais' (Não há um mecanismo no PHP para superglobais definidas pelo usuário) As superglobais são listadas abaixo. Entretanto, para uma explicação de seu conteúdo e detalhes sobre as variáveis predefinidas do PHP e sua natureza, veja a seção Variáveis Predefinidas. Veja também que todas as outras variáveis predefinidas antigas ($HTTP_*_VARS) ainda existem.

Se todos os indicadores não estiverem configurados no variables_order, seus arrays superglobais predefinidos respectivos estarão vazios.

Superglobais do PHP

$GLOBALS

Contém um referência para todas as variáveis que são atualmente disponíveis dentro do escopo global do script. As chaves desse array são os nomes das variáveis globais. $GLOBALS existe desde o PHP 3.

$_SERVER

Variáveis criadas pelo servidor web ou diretamente relacionadas ao ambiente de execução do script atual. Análogo ao antigo array $HTTP_SERVER_VARS (que ainda continua disponível, mas em decadência).

$_GET

Variáveis postadas para o script via método HTTP GET. Análogo ao antigo array $HTTP_GET_VARS (que ainda continua disponível, mas em decadência).

$_POST

Variáveis postadas para o script via método HTTP POST. Análogo ao antigo array $HTTP_POST_VARS (que ainda continua disponível, mas em decadência).

$_COOKIE

Variáveis postadas para o script via cookies HTTP. Análogo ao antigo array $HTTP_COOKIE_VARS (que ainda continua disponível, mas em decadência).

$_FILES

Variáveis postadas para o script via transferência de arquivos HTTP. Análogo ao antigo array $HTTP_POST_FILES (que ainda continua disponível, mas em decadência). Veja uploads via método POST para maiores informações.

$_ENV

Variáveis disponíveis no script do ambiente de execução. Análogo ao antigo array $HTTP_ENV_VARS (que ainda continua disponível, mas em decadência).

$_REQUEST

Variáveis postadas para o script por todas os mecanismos de input, e que não podem ter seu conteúdo garantido de qualquer forma. A presença e a ordem de inclusão das variáveis nesse array é definida de acordo com a diretiva de configuração variables_order. Este array não tem um equivalente nas versões anteriores do PHP 4.1.0. Veja também import_request_variables().

Nota: Quando executando na linha de comando , isto não inclui as entradas argv e argc; elas estão presentes no array $_SERVER.

$_SESSION

Variáveis que estão atualmente registradas na sessão do script. Análogo ao antigo array $HTTP_SESSION_VARS (que ainda continua disponível, mas em decadência). Veja a sessão funções de manipulação de Sessões para maiores informações.


Escopo de variáveis

O escopo de uma variável é o contexto onde ela foi definida. A maior parte das variáveis do PHP tem somente escopo local. Este escopo local inclui os arquivos incluídos. Por exemplo:

<?php
$a = 1;
include "b.inc";
?>

Aqui a variável $a estará disponível no script incluído b.inc. Entretanto, com as funções definidas pelo usuário, um escopo local é introduzido. Quaisquer variáveis utilizadas dento da função é por default limitada dentro do escopo local da função. Por exemplo:

<?php
$a = 1; /* escopo global */

function Teste()
{
    echo $a; /* referencia uma variável do escopo local (não definida) */
}

Test();
?>

Este script não produz nenhuma saída porque a instrução echo() refere-se a uma versão local da variável $a, e ela não tem nenhum valor assimilado nesse escopo. Essa é uma pequena diferença da linguagem C quando variáveis globais são automaticamente disponíveis para funções sem sobreescrever uma eventual definição local. Isto causa problemas quando as pessoas mudam inadivertidamente uma variável global. No PHP, as variáveis globais precisam ser declaradas globais dentro de uma função se ela vai ser utilizada naquela função. Um exemplo:

<?php
$a = 1;
$b = 2;

function Soma()
{
    global $a, $b;

    $b = $a + $b;
}

Soma();
echo $b;
?>

O script acima imprimirá "3". Declarando $a e $b globais na função, todas as referências a essas variáveis referem-se a versão global. Não há um limite para o número de variáveis globais que podem ser manipuladas por uma função.

Uma segunda maneira de acessar variáveis do escopo global é utilizando o array especial $GLOBALS definido pelo PHP. O exemplo anterior poderia ser rescrito como:

<?php
$a = 1;
$b = 2;

function Soma()
{
    $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"];
}

Soma();
echo $b;
?>

O array $GLOBALS é um array associativo onde o nome da variável global é a chave do array e o seu conteúdo da variável como o valor do elemento do array. Veja que $GLOBALS existe em qualquer escopo, isto porque $GLOBALS é uma superglobal. Segue um exemplo demonstrando o poder das superglobais:

<?php
function test_global()
{
    // A maioria das variaveis predefinidas nao sao 'super' e requerem
    // 'global' para serem disponiveis para funcoes em qualquer escopo.
    global $HTTP_POST_VARS;

    print $HTTP_POST_VARS['name'];

    // Superglobais são disponiveis em qualquer escopo e
    // nao precisam de 'global'. Superglobais existem
    // desde o PHP 4.1.0
    print $_POST['name'];
}
?>

Outro recurso importante do escopo de variáveis é a variável estática. Uma variável estática existe somente no escopo local da função, mas ela não perde seu valor quando o nível de execução do programa deixa o escopo. Considere o seguinte exemplo:

<?php
function Teste ()
{
    $a = 0;
    echo $a;
    $a++;
}
?>

Essa função é inútil partindo de que cada vez que ela é chamada, ela coloca em $a o valor 0 e imprime "0". A instrução $a++ , que aumenta o valor da variável não tem sentido desde que a função sai e a variável $a desaparece. Para faze-la mais útil como contadora sem deixar de perder o sua conta atual, a variável $a é declarada como estática:

<?php
function Teste()
{
    static $a = 0;
    echo $a;
    $a++;
}
?>

Agora, cada vez que a função Teste() for chamada ele imprimirá o valor de $a e o incrementará.

Variáveis estáticas fornecem uma solução ideal para funções recursivas. Uma função recursiva é aquela se chama a si mesma. Cuidados especiais precisam ser tomados quando escrevendo funções recursivas porque é possível que ela continue na recursão indefinidamente. Você tem de ter certeza que há uma maneira segura de terminar a recursão. A seguinte função recursiva conta até 10, utilizando a variável estática $count para saber quando parar:

<?php
function Teste()
{
    static $count = 0;

    $count++;
    echo $count;
    if ($count < 10) {
        Test ();
    }
    $count--;
}
?>

O Zend Engine 1, base do PHP4, implementa os modificadores static e global para variáveis em termos de referência. Por exemplo, uma variável global importada dentro do escopo de uma função com a instrução global atualmente cria uma referência para a variável global. Isto pode causar comportamentos impresíveis para os seguintes casos:

<?php
function test_global_ref() {
    global $obj;
    $obj = &new stdclass;
}

function test_global_noref() {
    global $obj;
    $obj = new stdclass;
}

test_global_ref();
var_dump($obj);
test_global_noref();
var_dump($obj);
?>

Executando esse exemplo você terá as seguites saídas:

NULL
object(stdClass)(0) {
}

Uma situação similar se aplica ao modificador static. Referências não são armazenadas estaticamente:

<?php
function &get_instance_ref() {
    static $obj;

    echo "Objeto estatico: ";
    var_dump($obj);
    if (!isset($obj)) {
        // Assimila uma referencia a variavel estatica
        $obj = &new stdclass;
    }
    $obj->property++;
    return $obj;
}

function &get_instance_noref() {
    static $obj;

    echo "Objeto estatico: ";
    var_dump($obj);
    if (!isset($obj)) {
        // Assimila o objeto para a veriavel estatica
        $obj = new stdclass;
    }
    $obj->property++;
    return $obj;
}

$obj1 = get_instance_ref();
$still_obj1 = get_instance_ref();
echo "\n";
$obj2 = get_instance_noref();
$still_obj2 = get_instance_noref();
?>

Executando esse exemplo você terá as seguites saídas:

Objeto estatico: NULL
Objeto estatico: NULL

Objeto estatico: NULL
Objeto estatico: object(stdClass)(1) {
  ["property"]=>
  int(1)
}

Este exemplo demonstra que quando assimilando uma referência para uma variável estática, ela não se lembra quando você chama a função &get_instance_ref() uma segunda vez.


Variáveis variáveis

As vezes é conveniente poder trabalhar com variáveis variáveis. Isto é, nomes de variáveis que pode ser criadas e utilizadas dinamicamente. Uma variável normal é criada numa instrução como:

<?php
$a = "hello";
?>

Uma variável variável pega o valor de uma variável e a trata como o nome de uma variável. No exemplo acima, hello pode ser utilizada como o nome de uma variável utilizando dois sinais de cifrão:

<?php
$$a = "world";
?>

Neste ponto, duas variáveis foram definidas e preservadas na árvore de símbolos do PHP: $a contendo "hello" e $hello contendo "world". Da mesma forma, esta instrução:

<?php
echo "$a ${$a}";
?>

produz a mesma saida que:

<?php
echo "$a $hello";
?>

no caso: hello world.

Para poder utilizar variáveis variáveis com arrays, você precisa resolver um problema de ambigüidade. Assim, se você escrever $$a[1] então o interpretador pode entender que você quer usar $a[1] como uma variável ou que você quer usar $$a como uma variável e [1] como o índice dessa variável. A sintaxe para resolver essa ambigüidade é ${$a[1]} para o primeiro caso e ${$a}[1] para o segundo.

Verifique que variáveis variáveis não podem ser utilizadas com os novos arrays superglobais. Isto significa que você não pode fazer coisas como ${$_GET}. Se você está procurando uma maneira de manipular as superglobais como as antigas HTTP_*_VARS, você deve tentar referenciá-las.


Variáveis externas ao PHP

Formulários HTML (GET and POST)

Quando um formulário é submetido para um script PHP, qualquer variável do formulário será automaticamente disponível para o script. Há várias maneiras de acessar estas informações, por exemplo:

Exemplo 8-1. Um formulário HTML simples

<form action="foo.php" method="post">
    Nome:  <input type="text" name="username"><br>
    Email: <input type="text" name="email"><br>
    <input type="submit" name="submit" value="Me aperte!">
</form>

Dependendo da configuração local e suas preferencias pessoais, essas são as vias pela qual você pode acessar os dados de seus formulários:

Exemplo 8-2. Acessando dados de um formulário HTML via POST

<?php
// Disponível desde o PHP 4.1.0

   print $_POST['username'];
   print $_REQUEST['username'];

   import_request_variables('p', 'p_');
   print $p_username;

// Disponível deste o PHP 3.

   print $HTTP_POST_VARS['username'];

// Disponível se a diretiva register_globals = on.
// Desde o PHP 4.2.0 o valor default de register_globals é off
// Usar/manter esse método é preferível.

   print $username;
?>

Utilizar um formulário GET é similar, exceto que você use a variável GET predefinida. O metodo GET obtem os dados da QUERY_STRING (a informação depois do '?' numa URL). Então, por exemplo, http://www.example.com/test.php?id=3 contém os dados GET que serão acessíveis com $_GET['id']. Veja também $_REQUEST e import_request_variables().

Nota: Arrays superglobais, como $_POST e $_GET, estão disponíveis desde o PHP 4.1.0.

Como explicado, antes do PHP 4.2.0 o valor default de register_globals era on. E no PHP ele era sempre on. A comunidade PHP está encorajando todos a não alterarem essa diretiva, assumindo-a sempre como off e codificando em conformidade com isso.

Nota: A diretiva de configuração magic_quotes_gpc afeta os valores de GET, POST e Cookies. Se estiver ativada, o valor (It's "PHP!") se tornará automaticamente (It\'s \"PHP!\"). Escaping é necessário para inserção em bancos de dados. Veja também addslashes(), stripslashes() e magic_quotes_sybase.

O PHP entende arrays no contexto de variáveis de formulários (veja o FAQ relacionado). Você pode, por exemplo, agrupar variáveis relacionadas juntas, ou usar esse recurso para receber valores de um campo de seleção múltipla. Por exemplo, podemos ter um formulario que manda informações para si mesmo até um comando submetido para mostrar todos os dados.

Exemplo 8-3. Variáveis de formulários mais complexos

<?php
if ($HTTP_POST_VARS['action'] == 'submitted') {
    print '<pre>';

    print_r($HTTP_POST_VARS);
    print '<a href="'. $HTTP_SERVER_VARS['PHP_SELF'] .'">De novo</a>';

    print '</pre>';
} else {
?>
<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
    Nome:  <input type="text" name="personal[name]"><br>
    Email: <input type="text" name="personal[email]"><br>
    Cerveja: <br>
    <select multiple name="beer[]">
        <option value="antartica">Antartica</option>
        <option value="brahma">Brahma</option>
        <option value="skol">Skol</option>
    </select><br>
    <input type="hidden" name="action" value="submitted">
    <input type="submit" name="submit" value="Enviar dados!">
</form>
<?php
}
?>

No PHP 3, os arrays variáveis de formulários eram limitados a uma dimensão. No PHP 4, essa restrição não existe mais.


Nomes de variáveis SUBMIT IMAGE

Quando submetendo um formulário, é possível de se utilizar imagens ao invés do botão de submit padrão com uma tag do tipo:

<input type="image" src="image.gif" name="sub">

Quando o usuário clica em algum lugar da imagem, o formulário que o acompanha é transmitido para o servidor com duas variáveis adicionais, sub_x e sub_y. Eles contém a coordenadas do clique do usuário na imagem. Os mais experientes percebem que os atuais nomes dessas variáveis enviados pelo browser contém um ponto ao invés de um sublinhado, mas o PHP converte o ponto para um sublinhado automaticamente.


Cookies HTTP

O PHP suporta transparentemente cookies HTTP como os definidos pela especificação da Netscape. Cookies são um mecanismo de armazenamento de dados no browser cliente e permite o rastreamento ou identificação do retorno de usuários. Você pode criar cookies com a função setcookie(). Cookies são parte do header HTTP, então, a função setcookie() precisa ser chamada antes de qualquer saída ser enviada ao browser. Esta é a mesma restrição da função header(). Dados de cookies são disponíveis nos arrays de dados de cookies apropriados, como $_COOKIE, $HTTP_COOKIE_VARS como também em $_REQUEST. Veja o manual de setcookie() para mais detalhes e exemplos.

Se você deseja assimilar vários valores para uma única variável cookie, você pode fazer dele um array:

<?php
  setcookie("MeuCookie[foo]", "Testando 1", time()+3600);
  setcookie("MeuCookie[bar]", "Testando 2", time()+3600);
?>

Isso irá criar dois cookies separados enquanto MeuCookie será um único array em seu script. Se você quiser colocar em apenas um cookie vários valores, considere utilizar serialize() ou explode() nos valores primeiro.

Note que um cookie substituirá um anterior com o mesmo nome em seu browser mesmo se o nome ou o caminho for diferente. Então, para uma aplicação de carrinho de compras em que você quer ter um contador e repassá-lo:

Exemplo 8-4. Exemplo setcookie()

<?php
$count++;
setcookie("count", $count, time()+3600);
setcookie("Cart[$count]", $item, time()+3600);
?>

Pontos em nomes de variáveis postadas

Normalmente o PHP não altera o nome de variáveis quando elas são passadas para o script. Entretanto, é necessário notar que o ponto (ponto final) não é um caracter válido no nomes de variáveis do PHP. Para ilustrar, veja o seguinte exemplo:
<?php
$varname.ext;  /* nome de variável inválido */
?>
Dessa forma, o interpretador entende isso como uma variável nomeada $varname, seguida do operador de concatenação de strings, seguida de um identificador (uma string não delimitada que não bate com nenhuma palavra chave ou reservada) 'ext'. Obviamente, isso não tem os resultados pretendidos.

Nessa situação, é importante saber que o PHP automaticamente substituirá qualquer ponto nos nomes de variáveis recebidas com sublinhados.


Determinando o tipo das variáveis

Porque o PHP determina os tipos de variáveis e faz conversões (geralmente) quando necessárias, nem sempre é óbvio o tipo de uma variável tem em todos os momentos. O PHP incluí várias funções que permitem determinar qual o tipo de uma variável, por exemplo: gettype(), is_array(), is_float(), is_int(), is_object(), e is_string(). Veja também o capítulo Tipos.


Capítulo 9. Constantes

Uma constante é um identificador (nome) para um valor simples. Como o nome sugere, esse valor não pode mudar durante a execução do script (as 'constantes mágicas' __FILE__ e __LINE__ parecem ser uma exceção a essa regra, mas elas não são realmente constantes). As constantes são sensíveis ao caso por padrão. Por convenção, o nomes de constantes são sempre em maiúsculas.

O nome de uma constante tem as mesmas regras de qualquer identificador no PHP. Um nome de constante válida começa com uma letra ou sublinhado, seguido por qualquer número de letras, números ou sublinhados. Em expressões regulares, ela pode ser representada por: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*

Nota: Para nossos exemplos, as letras a-z, A-Z e os caracteres ASCII do 127 ao 255 (0x7f-0xff).

O escopo de uma constante é global. Você pode acessá-la em qualquer lugar em seu script sem se preocupar com seus escopo.


Sintaxe

Você pode definir uma constante utilizando-se da função define(). Quando uma constante é definida, ela não pode ser mais modificada ou anulada.

Somente dados escalares (boolean, integer, float e string) pode ser colocados em constantes.

Você pode obter o valor de uma constante simplismente especificando seu nome. Diferentemente de variáveis, você não pode prefixar uma constante com um sinal de $. Você também pode utilizar a função constant() para ler o valor de uma constante, se você precisar obter seu valor dinamicamente. Utilize get_defined_constants() para obter a lista de todas as constantes definidas.

Nota: As constantes e variáveis (globais) estão em espaços de nomes diferentes. Isto implica, por exemplo, que TRUE e $TRUE são geralmente diferentes.

Se você usar uma constante indefinida, o PHP assume o nome da constante como seu próprio valor. Uma notice será informada quando isso acontecer. Use a função defined() se você precisar saber se uma constante está definida ou não.

Estas são as diferenças entre constantes e variáveis:

  • Constantes não podem ter um sinal de cifrão ($) antes delas;

  • Constantes só podem ser definidas utilizando a função define(), e não por simples assimilação;

  • Constantes podem ser definidas e acessadas de qualquer lugar sem que a regras de escopo de variáveis seja aplicadas;

  • Constantes não podem ser redefinidas ou eliminadas depois que elas são criadas; e

  • Constantes só podem conter valores escalares.

Exemplo 9-1. Definindo Constantes

<?php
define("CONSTANT", "Hello world.");
echo CONSTANT; // imprime "Hello world."
echo Constant; // imprime "Constant" e gera um alerta notice.
?>


Constantes Predefinidas

O PHP fornece um grande número de constantes predefinidas para qualquer script que ele execute. A maioria dessas constantes, entretanto, são criadas por várias extensões, e somente estarão presentes quando essas extensões estiverem disponíveis, por carregamento dinamico ou por compilação direta.

A lista de constantes predefinidas está disponível na seção Constantes Predefinidas.


Capítulo 10. Expressões

Expressões são as peças de construção mais importantes do PHP. No PHP, quase tudo o que você escreve são expressões. A maneira mais simples e ainda mais precisa de definir uma expressão é "tudo o que tem um valor".

As formas mais básicas de expressões são constantes e variáveis. Quando você digita "$a = 5", você está atribuindo '5' para $a. '5', obviamente, tem o valor 5, ou, em outras palavras, '5' é uma expressão com o valor 5 (neste caso, '5' é uma constante inteira).

Depois desta atribuição, você pode esperar que o valor de $a seja 5 também, assim se você escrever $b = $a, você pode esperar que $b se comporte da mesma forma que se você escrevesse $b = 5. Em outras palavras, $a é uma expressão com valor 5 também. Se tudo funcionou bem, isto é exatamente o que acontecerá.

Exemplos ligeiramente mais complexos para expressões são as funções. Por exemplo, considere a seguinte função:

function foo ()
{
    return 5;
}

Assumindo que você está familiarizado com o conceito de funções (se não estiver, dê uma olhada no capítulo sobre funções), você pode assumir que digitar $c = foo() é essencialmente a mesma coisa que escrever $c = 5, e você está certo. Funções são expressões com o valor igual ao seu valor de retorno. Como foo() retorna 5, o valor da expressão 'foo()' é 5. Geralmente, as funções não retornam apenas um valor estático, mas computam algo.

Obviamente, os valores em PHP não têm que ser inteiros, e muito freqüentemente eles não são. O PHP suporta três tipos de valores escalares: valores inteiros, valores de ponto flutuante e valores de string (valores escalares são valores que você não pode quebrar em pedaços menores, diferente de matrizes, por exemplo). O PHP também suporta dois tipos compostos (não-escalares): matrizes e objetos. Cada um destes tipos de valor podem ser atribuídos a variáveis ou retornadas de funções.

Até agora, os usuários de PHP/FI 2 não sentiriam qualquer mudança. Porém, o PHP traz expressões bem mais novas, da mesma forma que muitas outras linguagens. O PHP é uma linguagem orientada a expressões, no sentido de que quase tudo são expressões. Considere o exemplo com o qual já lidamos, '$a = 5'. É fácil ver que há dois valores envolvidos aqui, o valor da constante inteira '5', e o valor de $a que está sendo atualizado para 5 também. Mas a verdade é que há um valor adicional envolvido, e que é o próprio valor da atribuição. A própria atribuição é avaliada com o valor atribuído, neste caso 5. Na prática, significa que '$a = 5', independente do que faça, é uma expressão com o valor 5. Portanto, escrever algo como '$b = ($a = 5)' é como escrever '$a = 5; $b = 5;' (um ponto-e-vírgula marca o fim do comando). Como atribuições são analisadas da direita para a esquerda, você também pode escrever '$b = $a = 5'.

Outro bom exemplo de orientação de expressão é o pré e o pós-incremento e decremento. Usuários de PHP/FI 2 e muitas outras linguagens podem estar familiarizados com a notação de variável++ e variável--. Estes são os operadores de incremento e decremento. No PHP/FI 2, o comando '$a++' não tem valor (não é uma expressão), e portanto você não pode atribuir desta forma ou usá-la de jeito nenhum. O PHP evoluiu a capacidade de incremento/decremento criando estas expressões também, como em C. Em PHP, como em C, há dois tipos de incremento - pré-incremento e pós-incremento. Tanto o pré-incremento quanto o pós-incremento, essencialmente, incrementam variáveis, e o efeito sobre a variável é idêntico. A diferença é com o valor da expressão de incremento. O pré-incremento, que é escrito '++$variavel', é avaliado como o valor de incremento (o PHP incrementa a variável antes de ler seu valor, por isso o nome pré-incremento). O pós-incremento, que é escrito '$variavel++' é avaliado como o valor original da variável, antes de ser incrementada (o PHP incrementa a variável depois de ler seu valor, por isso o nome 'pós-incremento').

Um tipo muito comum de expressão são expressões de comparação. Estas expressões são avaliadas como 0 ou 1, significando FALSE ou TRUE, respectivamente. O PHP suporta > (maior que), >= (maior ou igual), == (igual), != (diferente), < (menor que) e <=(menor ou igual). Estas expressões são usadas mais freqüentemente dentro de instruções condicionais, como em comandos if.

O último exemplo de expressões com que nós vamos lidar aqui são as expressões combinadas operador-atribuição. Você já sabe que se você quer incrementar $a de 1, você só precisa escrever '$a++' ou '++$a'. Mas e se você quiser somar mais que um a ele, por exemplo 3? Você poderia escrever '$a++' várias vezes, mas esta obviamente não é uma forma muito eficiente ou confortável. Uma prática muito mais comum é escrever '$a = $a + 3'. '$a + 3' é avaliada como o valor de $a mais 3, e é atribuído de volta a $a, que resulta em incrementar $a de 3. Em PHP, como em várias outras linguagens como o C, você pode escrever isto de uma forma mais curta, que com o tempo se torna mais limpa e rápida de se entender, também. Somar 3 ao valor corrente de $a pode ser escrito '$a +=3'. Isto significa exatamente "pegue o valor de $a, some 3 a ele, e atribua-o de volta a $a." Além de ser mais curto e mais limpo, isto também resulta em execução mais rápida. O valor de '$a += 3', como o valor de uma atribuição regular, é o valor atribuído. Note que NÃO é 3, mas o valor combinado de $a mais 3 (este é o valor que é atribuído a $a). Qualquer operador de dois parâmetros pode ser usado neste modo operador-atribuição, por exemplo '$a -= 5' (subtrai 5 do valor de $a), '$b *= 7' (multiplica o valor de $b por 7), etc.

Há mais uma expressão que podem parecer estranha se você não a viu em outras linguagens, o operador condicional ternário:

$primeira ? $segunda : $terceira

Se o valor da primeira sub-expressão é verdadeiro (TRUE, não-zero), então a segunda sub-expressão é avaliada, e este é o resultado da expressão condicional. Caso contrário, a terceira sub-expressão é avaliada e este é o valor.

O seguinte exemplo deve ajudá-lo a entender um pouco melhor pré e pós-incremento e expressões em geral:

function double($i)
{
    return $i*2;
}
$b = $a = 5;        /* atribui o valor cinco às variáveis $a e $b */
$c = $a++;          /* pós-incremento, atribui o valor original de $a
                       (5) para $c */
$e = $d = ++$b;     /* pré-incremento, atribui o valor incrementado de
                       $b (6) a $d e $e */

/* neste ponto, tanto $d quanto $e são iguais a 6 */

$f = double($d++);  /* atribui o dobro do valor de $d <emphasis>antes</emphasis>
                       do incremento, 2*6 = 12 a $f */
$g = double(++$e);  /* atribui o dobro do valor de $e <emphasis>depois</emphasis>
                       do incremento, 2*7 = 14 a $g */
$h = $g += 10;      /* primeiro, $g é incrementado de 10 e termina com o valor
                       24. o valor da atribuição (24) é então atribuído
                       a $h, e $h termina com o valor 24 também. */

No começo do capítulo, nós dissemos que descreveríamos os vários tipos de comandos, e como prometido, expressões podem ser comandos. Porém, nem toda expressão é um comando. Neste caso, um comando tem a forma 'expr' ';', ou seja, uma expressão seguida de ponto-e-vírgula. E '$b=$a=5;', $a=5 é uma expressão válida, mas não é um comando por si só. '$b=$a=5;' porém é um comando válido.

Uma última coisa que vale mencionar é o valor-verdade de expressões. Em muitos eventos, principalmente em instruções condicionais e loops, você não está interessado no valor específico da expressão, mas somente se ela significa TRUE ou FALSE (o PHP não tem um tipo booleano dedicado). As constantes TRUE e FALSE (insensitivas ao caso) são seus dois valores booleanos possíveis. As vezes uma expressão é automaticamente convertida para um booleano. Veja a seção sobre type-casting para detalhes de como isso é feito.

O PHP fornece uma implementação completa e poderosa de expressões, e a completa documentação dela vai além do escopo deste manual. Os exemplos acima devem dar a você uma boa idéia sobre o que são as expressões e como você pode construir expressões úteis. Através do restante do manual nós escreveremos expr ou expressao para indicar qualquer expressão PHP válida.


Capítulo 11. Operadores


Precedência de Operadores

A precedência de um operador especifica quem tem mais prioridade quando há duas delas juntas. Por exemplo, na expressão, 1 + 5 * 3, a resposta é 16 e não 18 porque o operador de multiplicação ("*") tem prioridade de precedência que o operador de adição ("+"). Parênteses podem ser utilizados para forçar a precedência, se necessário. Assim, (1 + 5) * 3 é avaliado como 18.

A tabela seguinte mostra a precedência dos operadores, da menor precedência para a maior.

Tabela 11-1. Precedência dos operadores

AssociaçãoOperador
esquerda,
esquerdaor
esquerdaxor
esquerdaand
direitaprint
esquerda = += -= *= /= .= %= &= |= ^= ~= <<= >>=
esquerda? :
esquerda||
esquerda&&
esquerda|
esquerda^
esquerda&
não associativo== != === !==
não associativo< <= > >=
esquerda<< >>
esquerda+ - .
esquerda* / %
direita! ~ ++ -- (int) (float) (string) (array) (object) @
direita[
não associativonew


Operadores Aritméticos

Lembra-se da aritmética básica da escola? Estes operadores funcionam exatamente como aqueles.

Tabela 11-2. Operadores Aritméticos

ExemploNomeResultado
$a + $bAdiçãoSoma de $a e $b.
$a - $bSubtraçãoDiferença entre $a e $b.
$a * $bMultiplicaçãoProduto de $a e $b.
$a / $bDivisãoQuociente de $a por $b.
$a % $bMóduloResto de $a dividido por $b.

O operador de divisão ("/") sempre retorna um valor com ponto flutuante, mesmo que os dois operandos sejam inteiros (ou strings que sejam convetidos para inteiros).


Operadores de Atribuição

O operador básico de atribuição é "=". A sua primeira inclinação deve ser a de pensar nisto como "é igual". Não. Isto quer dizer, na verdade, que o operando da esquerda recebe o valor da expressão da direita (ou seja, "é configurado para").

O valor de uma expressão de atribuição é o valor atribuído. Ou seja, o valor de "$a = 3" é 3. Isto permite que você faça alguns truques:

$a = ($b = 4) + 5; // $a é igual a 9 agora e $b foi configurado como 4.

Além do operador básico de atribuição, há "operadores combinados" para todos os operadores binários, aritméticos e de string, que permitem a você pegar um valor de uma expressão e então usar seu próprio valor para o resultado daquela expressão. Por exemplo:

$a = 3;
$a += 5; // configura $a para 8, como se disséssemos: $a = $a + 5;
$b = "Bom ";
$b .= "Dia!"; // configura $b para "Bom Dia!", como em $b = $b . "Dia!";

Note que a atribuição copia a variável original para a nova (atribuição por valor), assim a mudança de uma não afeta a outra. Isto pode ter relevância se você precisa copiar algo como uma grande matriz dentro de um loop longo. O PHP 4 suporta atribuições por referência, usando a sintaxe $var = &$outra_var;, mas isto não é possível no PHP3. 'Atribuição por referência' significa que ambas as variáveis acabam apontando para os mesmos dados, e nada é copiado para lugar nenhum. Para aprender mais sobre referências, leia Referências explicadas.


Operador Bit-a-bit

Operadores bit-a-bit permitem que você acione ou desligue bits específicos dentro de um inteiro. Se ambos os parâmetros da esquerda e da direita forem strings, esses operadores irão trabalhar nos caracteres dessa string.

<?php
    echo 12 ^ 9; // Imprime '5'

    echo "12" ^ "9"; // Imprime o caracter de volta (backspace - ASCII 8)
                     // ('1' (ASCII 49)) ^ ('9' (ASCII 57)) = 8

    echo "hallo" ^ "hello"; // Imprime os valores ASCII 0 4 0 0 0
                            // 'a' ^ 'e' = 4
?>

Tabela 11-3. Operadores Bit-a-bit

ExemploNomeResultado
$a & $bEOs bits que estão ativos tanto em $a quanto em $b são ativados.
$a | $bOUOs bits que estão ativos em $a ou em $b são ativados.
$a ^ $bXOR Os bits que estão ativos em $a ou em $b, mas não em ambos, são ativados.
~ $aNÃO Os bits que estão ativos em $a não são ativados, e vice-versa.
$a << $bDeslocamento à esquerda Desloca os bits de $a $b passos para a esquerda (cada passo significa "multiplica por dois")
$a >> $bDeslocamento à direita Desloca os bits de $a $b passos para a direita (cada passo significa "divide por dois")

Operadores de Comparação

Operadores de comparação, como os seus nomes implicam, permitem que você compare dois valores.

Tabela 11-4. Operadores de comparação

ExemploNomeResultado
$a == $bIgualVerdadeiro (TRUE) se $a é igual a $b.
$a === $bIdêntico Verdadeiro (TRUE) se $a é igual a $b, e eles são do mesmo tipo (somente para PHP4).
$a != $bDiferenteVerdadeiro se $a não é igual a $b.
$a <> $bDiferenteVerdadeiro se $a não é igual a $b.
$a !== $bNão idêntico Verdadeiro de $a não é igual a $b, ou eles não são do mesmo tipo (somente para o PHP4).
$a < $bMenor queVerdadeiro se $a é estritamente menor que $b.
$a > $bMaior queVerdadeiro se $a é estritamente maior que $b.
$a <= $bMenor ou igualVerdadeiro se $a é menor ou igual a $b.
$a >= $bMaior ou igualVerdadeiro se $a é maior ou igual a $b.

Outro operador condicional é o operador "?:" (ou trinário), que opera como no C e em muitas outras linguagens.

(expr1) ? (expr2) : (expr3);

Esta expressão avalia para expr2 se expr1 é avaliada como TRUE, ou expr3 se expr1 é avaliada como FALSE.


Operadores de controle de erro

O PHP suporta um operador de controle de erro: o sinal 'arroba' (@). Quando ele precede uma expressão em PHP, qualquer mensagem de erro que possa ser gerada por aquela expressão será ignorada.

Se o recurso track_errors estiver habilitado, qualquer mensagem de erro gerada pela expressão será gravada na variável global $php_errormsg. Esta variável será sobrescrita em cada erro, assim verifique-a constantemente se você quiser usá-la.

<?php
/* Erro de arquivo intencional */
$my_file = @file ('arquivo_nao_existente') ou
    die ("Falha abrindo arquivo: '$php_errormsg'");

// Isto funciona para qualquer expressão, não apenas para funções:
$value = @$cache[$key];
// você não receberá nenhum aviso se a chave $key não existir.

?>

Nota: O operador @ funciona somente nas expressões. Uma regra simples para lembrar: se você pode pegar o valor de alguma coisa, você pode prefixar isso com o @. Assim, você pode prefixar chamadas de variáveis, funções e include()s, constantes e afins. Você não pode prefixar definições de funções ou classe, estruturas condicionais como o if, foreach e assim por diante.

Veja também: error_reporting().

Atenção

Atualmente, o operador de controle de erro "@" sempre desativa mensagens de erro, mesmo para erros críticos, que terminam a execução de scripts. Além de outras coisas, isto significa que se você usar "@" para suprimir erros de certas funções e elas não estiverem disponíveis ou com tipos incorretos, o script vai parar exatamente aí sem nenhuma indicação da razão.


Operadores de Execução

O PHP suporta um operador de execução: acentos graves (``). Note que não são apóstrofes! O PHP tentará executar o conteúdo dos acentos graves como um comando do shell; a saída será retornada (isto é, ela não será simplesmente descarregada para a saída; ela pode ser atribuída a uma variável).

$output = `ls -al`;
echo "<pre>$output</pre>";

Nota: O operador de execução fica desabilitado quando o safe mode está ativo ou shell_exec() está desabilitado.

Veja também: escapeshellcmd(), exec(), passthru(), popen(), shell_exec() e system().


Operadores de Incremento/Decremento

O PHP suporta operadores de pré e pós-incremento e decremento no estilo C.

Tabela 11-5. Operadores de Incremento/Decremento

ExemploNomeEfeito
++$aPré-incrementoIncrementa $a em um, e então retorna $a.
$a++Pós-incrementoRetorna $a, e então incrementa $a em um.
--$aPré-decrementoDecrementa $a em um, e então retorna $a.
$a--Pós-decrementoRetorna $a, e então decrementa $a em um.

Aqui está um script de exemplo simples:

<?php
echo "<h3>Pós-incremento</h3>";
$a = 5;
echo "Deve ser 5: " . $a++ . "<br />\n";
echo "Deve ser 6: " . $a . "<br />\n";

echo "<h3>Pré-incremento</h3>";
$a = 5;
echo "Deve ser 6: " . ++$a . "<br />\n";
echo "Deve ser 6: " . $a . "<br />\n";

echo "<h3>Pós-decremento</h3>";
$a = 5;
echo "Deve ser 5: " . $a-- . "<br />\n";
echo "Deve ser 4: " . $a . "<br />\n";

echo "<h3>Pré-decremento</h3>";
$a = 5;
echo "Deve ser 4: " . --$a . "<br />\n";
echo "Deve ser 4: " . $a . "<br />\n";
?>


Operadores Lógicos

Tabela 11-6. Operadores Lógicos

ExemploNomeResultado
$a and $bEVerdadeiro (TRUE) se tanto $a quanto $b são verdadeiros.
$a or $bOUVerdadeiro se $a ou $b são verdadeiros.
$a xor $bXORVerdadeiro se $a ou $b são verdadeiros, mas não ambos.
! $aNÃOVerdadeiro se $a não é verdadeiro.
$a && $bEVerdadeiro se tanto $a quanto $b são verdadeiros.
$a || $bOUVerdadeiro se $a ou $b são verdadeiros.

A razão para as duas variantes dos operandos "and" e "or" é que eles operam com precedências diferentes. (Veja Precedência de Operadores.)


Operadores de String

Há dois operadores de string. O primeiro é o operador de concatenação ('.'), que retorna a concatenação dos seus argumentos direito e esquerdo. O segundo é o operador de atribuição de concatenação ('.='), que acrescenta o argumento do lado direito no argumento do lado esquerdo. Veja em Operadores de Atribuição para mais informações.

$a = "Olá ";
$b = $a . "mundo!"; // agora $b contém "Olá mundo!"

$a = "Olá ";
$a .= "mundo!";     // agora $a contém "Olá mundo!"


Operadores de Arrays

O único operador de arrays no PHP é o +. Ele acrescenta o array informado à direita no array informado à esquerda, onde chaves duplicadas NÃO são sobrescritos.

$a = array("a" => "maçã", "b" => "banana");
$b = array("a" =>"pêra", "b" => "framboesa", "c" => "morango");

$c = $a + $b;

var_dump($c);
array(3) {
  ["a"]=>
  string(5) "maçã"
  ["b"]=>
  string(6) "banana"
  ["c"]=>
  string(6) "morango"
}


Capítulo 12. Estruturas de Controle

Qualquer script PHP é construído por uma série de instruções. Uma instrução pode ser uma atribuição, uma chamada de função, um 'loop', uma instrução condicional, ou mesmo uma instrução que não faz nada(um comando vazio). Instruções geralmente terminam com um ponto e vírgula. Além disso, as instruções podem ser agrupados em um grupo de comandos através do encapsulamento de um grupo de comandos com chaves. Um grupo de comandos é uma instrução também. Os vários tipos de instruções são descritos neste capítulo.


if

A construção if é uma das mais importantes implementações de muitas linguagens, incluindo o PHP. Ela permite a execução condicional de fragmentos de código. O PHP implementa uma estrutura if que é similar àquela do C:

if (expressao)
    instrucoes

Como descrita na seção sobre expressões , expressao é avaliado por seu contexto Booleano. Se expressao for avaliado como TRUE, o PHP executará instrucoes, e se for avaliado como FALSE, ele será ignorado. Maiores informações sobre a avaliação para FALSE podem ser encontradas na seção Convertendo para Booleanos .

Os exemplos a seguir mostrariam que a é maior que b se $a for maior que $b:

if ($a > $b)
    print "a é maior que b";

Freqüentemente você vai querer ter mais que uma instrução seja executado condicionalmente. E é claro, não há necessidade de englobar cada instrução com uma cláusula if. Em vez disso, você pode colocar várias instruções em um agrupamento de comandos. Por exemplo, este código mostraria a é maior que b se $a for maior que $b, e então atribuiria o valor de $a para $b:

if ($a > $b) {
    print "a é maior que b";
    $b = $a;
}

Comandos if podem ser aninhados indefinidamente dentro de outros comandos if, o que faz com que você complete a flexibilidade para a execução condicional de várias partes do seu programa.


else

Freqüentemente você vai querer executar uma instrução se uma certa condição for encontrada, e uma instrução diferente se a condição não for encontrada. Isto é o que o else faz. else estende um comando if para executar uma instrução caso a expressão no comando if seja avaliada como FALSE. Por exemplo, o código a seguir mostraria a é maior que b se $a for maior que $b, e a NÃO é maior que b caso contrário:

if ($a > $b) {
    print "a é maior que b";
} else {
    print "a NÃO é maior que b";
}

O comando else só é executado se a expressão if for avaliada como FALSE, e se havendo qualquer expressão elseif, somente se todas elas forem avaliadas como FALSE também (veja elseif).


elseif

elseif, como seu nome sugere, é uma combinação de if e else. Da mesma forma que o else, ele estende um comando if para executar uma instrução diferente no caso de a expressão if original ser avaliada como FALSE. Porém, ao contrário de else, ele executará aquela expressão alternativa somente se a expressão condicional do elseif for avaliada como TRUE. Por exemplo, o código a seguir mostraria a é maior que b, a é igual a b ou a é menor que b:

if ($a > $b) {
    print "a é maior que b";
} elseif ($a == $b) {
    print "a é igual a b";
} else {
    print "a é menor que b b";
}

Podem haver vários elseifs dentro da mesma instrução if. A primeira expressão elseif (se houver) que for avaliada como TRUE será executada. No PHP, você também pode escrever 'else if' (em duas palavras) e o comportamento será idêntico a um 'elseif' (em uma só palavra). O significado sintático é ligeiramente diferente (se você está familiarizado com C, eles tem o mesmo comportamento), mas no final de contas ambos teriam exatamente o mesmo comportamento.

O comando elseif só é executado se a expressão if precedente e quaisquer expressões elseif anteriores forem avaliadas como FALSE, e a expressão elseif atual for avaliada como TRUE.


Sintaxe alternativa para estruturas de controle

O PHP oferece uma sintaxe alternativa para algumas das suas estruturas de controle: if, while, for, foreach e switch. Em cada caso, a forma básica da sintaxe alternativa é mudar o sinal de abertura para dois-pontos (:) e o sinal de fechamento para endif;, endwhile;, endfor;, endforeach; ou endswitch;, respectivamente.

<?php if ($a == 5): ?>
A é igual a 5
<?php endif; ?>

No exemplo acima, o bloco HTML "A é igual a 5" está aninhado dentro de uma instrução if escrito na sintaxe alternativa. O bloco HTML será mostrado somente se $a é igual a 5.

A sintaxe alternativa se aplica a else e elseif também. A seguir temos uma estrutura if com elseif e else no formato alternativo:

if ($a == 5):
    print "a igual a 5";
    print "...";
elseif ($a == 6):
    print "a igual a 6";
    print "!!!";
else:
    print "a não é nem 5 nem 6";
endif;

Veja também: while, for e if para mais exemplos.


while

Loops while são o tipo mais simples de criar um 'loop' em PHP. Eles se comportam como seus compatíveis em C. O formato básico de um comando while é:

while (expressao) instrucoes

O significado de um comando while é simples. Ele pede que o PHP execute os comandos aninhados repetidamente, enquanto a expressão do while é avaliada como TRUE. O valor da expressão é verificada cada vez que se passa no começo do 'loop', desta forma, mesmo que este valor mude durante a execução do(s) comando(s) aninhado(s), a execução não parará até que o fim da iteração (cada vez que o PHP executa os comandos dentro do 'loop' é uma iteração). Às vezes, se a expressão while é avaliada como FALSE logo no início, o(s) comando(s) aninhado(s) não será(ão) rodado(s) nem uma vez sequer.

Como no comando if, você pode agrupar múltiplos comandos dentro do mesmo laço while englobando um grupo de instruções com chaves, ou usando a sintaxe alternativa:

while (expressao): instrucoes ... endwhile;

Os exemplos a seguir são idênticos, e ambos imprimem números de 1 to 10:

/* exemplo 1 */

$i = 1;
while ($i <= 10) {
    print $i++;  /* o valor impresso será
                    $i depois do acréscimo
                    (post-increment) */
}

/* exemplo 2 */

$i = 1;
while ($i <= 10):
    print $i;
    $i++;
endwhile;


do..while

Loops do..while são bem similares aos loops while, exceto pelo fato de que a condição é verificada no fim de cada iteração em vez de no começo. A diferença principal dos loops while regulares é que a primeira iteração de um loop do..while é garantidamente executada (a condição só é verificada no fim da iteração) enquanto que ele pode não rodar necessariamente em um loop while normal (a condição é verificada no começo de cada iteração, se ela é avaliada como FALSE logo no começo, a execução do loop terminaria imediatamente).

Há apenas uma sintaxe para loops do..while:

$i = 0;
do {
   print $i;
} while ($i>0);

O loop acima rodaria exatamente uma vez, desde que depois da primeira iteração, quando a condição é verificada, ela é avaliada como FALSE ($i não é maior que zero 0) e a execução do loop termina.

Usuários avançados de C podem estar familiarizados com o uso diferenciado do loop do..while, para permitir o fim da execução no meio dos blocos de código, englobando-os com do..while(0), e usando a instrução break . O fragmento de código a seguir demonstra isso:

do {
    if ($i < 5) {
        print "i não é grande o suficiente";
        break;
    }
    $i *= $factor;
    if ($i < $minimum_limit) {
        break;
    }
    print "i está Ok";

     ...process i...

} while(0);

Não se preocupe se você não entendeu isto da forma certa ou de jeito nenhum. Você pode codificar scripts simples ou mesmo poderosos sem usar essa 'opção'.


for

Loops for são os laços mais complexos em PHP. Eles se comportam como os seus compatíveis em C. A sintaxe de um loop for é:

for (expr1; expr2; expr3) instrucoes

A primeira expressão (expr1) é avaliada (executada) uma vez incondicionalmente no começo do loop.

No começo de cada iteração, expr2 é avaliada. Se ela é avaliada como TRUE, o loop continua e o(s) comando(s) aninhado(s) é(são) executado(s). Se é avaliada como FALSE, a execução do 'loop' termina.

No fim de cada iteração, expr3 é avaliada (executada).

Cada uma das expressões pode ser vazia. expr2 vazia significa que o loop pode rodar indefinidamente (PHP considera-a implicitamente como TRUE, como em C). Isto pode não ser tão inútil quanto você pode pensar, pois freqüentemente você pode querer terminar o 'loop' usando uma instrução break condicional em vez de usar a expressão-verdade do for.

Considere os seguintes exemplos. Todos eles mostram números de 1 a 10:

/* exemplo 1 */

for ($i = 1; $i <= 10; $i++) {
    print $i;
}

/* exemplo 2 */

for ($i = 1;;$i++) {
    if ($i > 10) {
        break;
    }
    print $i;
}

/* exemplo 3 */

$i = 1;
for (;;) {
    if ($i > 10) {
        break;
    }
    print $i;
    $i++;
}

/* exemplo 4 */

for ($i = 1; $i <= 10; print $i, $i++);

Obviamente, o primeiro exemplo parece ser o mais bonito (ou talvez o quarto), mas você pode perceber que a possível utilização de expressões vazias em laços for se torna prático em algumas ocasiões.

O PHP também suporta a "sintaxe de dois-pontos" alternativa para laços for:

for (expr1; expr2; expr3): instrucoes; ...; endfor;

Outras linguagens têm uma instrução foreach para varrer uma matriz ou tabela de hashing. O PHP 3 não tem uma construção deste tipo; O PHP 4 possui (veja foreach). No PHP 3, você pode combinar while com as funções list() e each() para obter o mesmo efeito. Veja a documentação para estas funções para exemplos.


foreach

O PHP4 (mas não o PHP3) inclui uma construção foreach, muito parecido com o PERL e outras linguagens. Isto simplesmente oferece uma maneira fácil de iterar sobre matrizes. Há duas sintaxes; a segunda é uma abreviatura, mas útil, da primeira:

foreach(expressao_array as $valor) instrucoes
foreach(expressao_array as $chave => $valor) instrucoes

A primeira forma varre uma dada matriz dada por expressao_array. Em cada 'loop', o valor do elemento corrente é atribuído a $valor e o ponteiro interno da matriz é avançado em uma posição (assim, no próxima iteração você estará olhando para o próximo elemento).

A segunda forma faz a mesma coisa, exceto pelo fato de que a chave do elemento atual será atribuído à variável $chave em cada iteração.

Nota: Quando o foreach inicia sua primeira execução, o ponteiro interno da matriz é zerado automaticamente para o primeiro elemento do array. Isto significa que você não precisa chamar reset() antes de um loop foreach .

Nota: Note também que foreach opera sobre uma cópia do array especificado, não o próprio array, portanto o ponteiro da array não é modificado como na instrução each(), que altera o elemento do array selecionado, mas isso não se reflete o array original.

Nota: foreach tem a habilidade de evitar mensagens de erro com '@'.

Você pode ter notado que os seguintes itens são funcionalmente idênticos:

reset ($arr);
while (list(, $value) = each ($arr)) {
    echo "Valor: $value<br>\n";
}

foreach ($arr as $value) {
    echo "Valor: $value<br>\n";
}

Os seguintes também são funcionalmente idênticos:

reset ($arr);
while (list($key, $value) = each ($arr)) {
    echo "Chave: $key; Valor: $value<br>\n";
}

foreach ($arr as $key => $value) {
    echo "Chave: $key; Valor: $value<br>\n";
}

Mais alguns exemplos para demonstrar os usos:

/* exemplo foreach 1: somente valores */

$a = array (1, 2, 3, 17);

foreach ($a as $v) {
   print "Valor atual de \$a: $v.\n";
}

/* exemplo foreach 2: valores (com as chaves impressas para ilustração) */

$a = array (1, 2, 3, 17);

$i = 0; /* para exemplo somente */

foreach($a as $v) {
    print "\$a[$i] => $v.\n";
    $i++;
}

/* exemplo foreach 3: chaves e valores */

$a = array (
    "um" => 1,
    "dois" => 2,
    "três" => 3,
    "dezessete" => 17
);

foreach($a as $k => $v) {
    print "\$a[$k] => $v.\n";
}

/* exemplo foreach 4: arrays multidimensionais */

$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

foreach($a as $v1) {
    foreach ($v1 as $v2) {
        print "$v2\n";
    }
}

/* exemplo foreach 5: arrays dinâmicos */

foreach(array(1, 2, 3, 4, 5) as $v) {
    print "$v\n";
}


break

break cancela a execução do comando for, foreach while, do..while ou switch atual.

break aceita um argumento numérico opcional que diz a ele quantas estruturas aninhadas englobadas devem ser quebradas.

$arr = array ('um', 'dois', 'três', 'quatro', 'PARE', 'cinco');
while (list (, $val) = each ($arr)) {
    if ($val == 'PARE') {
        break;    /* Você poderia colocar 'break 1;' aqui. */
    }
    echo "$val<br>\n";
}

/* Utilizando o argumento opcional. */

$i = 0;
while (++$i) {
    switch ($i) {
    case 5:
        echo "No 5<br>\n";
        break 1;  /* Sai somente do switch. */
    case 10:
        echo "No 10; saindo<br>\n";
        break 2;  /* Sai do switch e while. */
    default:
        break;
    }
}


continue

continue é usado dentro de estruturas de loops para saltar o resto da iteração do loop atual e continuar a execução no início da próxima iteração.

continue aceita um argumento numérico opcional que diz a ele de quantos níveis de loops aninhados ele deve saltar até o fim.

while (list ($key, $value) = each ($arr)) {
    if (!($key % 2)) { // pula itens pares
        continue;
    }
    do_something_odd ($value);
}

$i = 0;
while ($i++ < 5) {
    echo "Fora<br>\n";
    while (1) {
        echo "&nbsp;&nbsp;Meio<br>\n";
        while (1) {
            echo "&nbsp;&nbsp;Dentro<br>\n";
            continue 3;
        }
        echo "Isto nunca será exibido.<br>\n";
    }
    echo "Nem isso.<br>\n";
}


switch

A instrução switch é similar a uma série de instruções IFs seguidas. Em muitas ocasiões, você poderá ter que comparar a mesma variável (ou expressão) com muitos valores diferentes, executando códigos diferentes dependendo com qual valor ele se encaixar. É exatamente para isso que a instrução switch faz.

Os exemplos seguintes mostram duas maneiras diferentes de escrever a mesma coisa, uma utilizando uma série de iss e a outra utlizando a instrução switch:

if ($i == 0) {
    print "i igual a 0";
}
if ($i == 1) {
    print "i igual a 1";
}
if ($i == 2) {
    print "i igual a 2";
}

switch ($i) {
    case 0:
        print "i igual a 0";
        break;
    case 1:
        print "i igual a 1";
        break;
    case 2:
        print "i igual a 2";
        break;
}

É importante entender como a instrução switch funciona para evitar enganos. A instrução switch executa linha a linha (atualmente, instrução a instrução). No início, nenhum código é executado. Somente quando uma instrução case é encontrada com um valor que combina com a expressão do switch faz com que o PHP execute as instruções a partir daí. O PHP continua executando as instruções até o fim do bloco switch ou na primeira vez que encontrar uma instrução break. Se você não escrever uma instrução break no fim das instruções case, o PHP continuará executando os cases seguintes. Exemplo:

switch ($i) {
    case 0:
        print "i igual a 0";
    case 1:
        print "i igual a 1";
    case 2:
        print "i igual a 2";
}

Aqui, se $i é igual a zero, o PHP executará todas as instruções print!. Se $i é igual a 1, o PHP executará os últimas duas instruções print, e somente se $i for igual a 2, você terá o comportamento 'esperado' apenas onde 'i igual a 2' será mostrado. Então é importante não se esquecer das instruções break (e as vezes não colocá-las para esse resultado em certas circunstâncias).

Em uma instrução switch, a condição somente será avaliada e resultado comparado para cada instrução case. Em uma instrução elseif, a condição é avaliada novamente. Se sua condição é mais complicada que um simples comparação e/ou e dentro de um loop, um switch é mais rápido.

Um case pode não ter nenhuma instrução dentro, o que simplesmente passa o controle para o próximo case.

switch ($i) {
    case 0:
    case 1:
    case 2:
        print "i é menor que 3 mas não negativo";
        break;
    case 3:
        print "i é 3";
}

Um case especial é o default. Esse case é executado quando nenhum outro case combina. Ele precisa ser a última instrução case. Por exemplo:

switch ($i) {
    case 0:
        print "i igual a 0";
        break;
    case 1:
        print "i igual a 1";
        break;
    case 2:
        print "i igual a 2";
        break;
    default:
        print "i não é igual a 0, 1 ou 2";
}

A expressão avaliada pelo case precisa ser um tipo simples, ou seja, inteiros, números de ponto flutuante e strings. Arrays ou objetos não podem ser utilizados a não ser que eles impliquem num tipo simples.

A sintaxe alternativa para estruturas de controle é suportada para os switches. Para maiores informações, veja Sintaxe alternativa para estruturas de controle.

switch ($i):
    case 0:
        print "i igual a 0";
        break;
    case 1:
        print "i igual a 1";
        break;
    case 2:
        print "i igual a 2";
        break;
    default:
        print "i não é igual a 0, 1 ou 2";
endswitch;


declare

O construtor declare é utilizado para configurar diretivas de execução para blocos de código. A sintaxe do declare é similar a sintaxe de outros construtores de controle.

declare (diretiva) instrucao

A seção diretiva permite o comportamento do bloco declare a ser configurado. Atualmente somente uma diretiva é reconhecida: a diretiva ticks. (veja abaixo para maiores informações em diretiva ticks)

A parte instrucao do bloco declare será executada - como ela é executada e que efeitos colaterais que podem ocorrem durante a execução dependem da configuração diretiva.


Ticks

Um tick é um evento que ocorre para cada N níveis de instruções executados pelo interpretador com o bloco declare. O valor para N é especificado utilizando ticks=N nos blocos declare das seções diretiva.

O(s) evento(s) que ocorre(m) em cada tick são especificados utilizando register_tick_function(). Veja o exemplo abaixo para maiores detalhes. Note que mais de um evento pode ocorrer em cada tick.

Exemplo 12-1. Histórico de um trecho de código PHP

<?php
// Uma função que grava o tempo entre as chamadas
function profile ($dump = FALSE)
{
    static $profile;

    // Retorna os tempos preservados no histórico, então apaga
    if ($dump) {
        $temp = $profile;
        unset ($profile);
        return ($temp);
    }

    $profile[] = microtime ();
}

// Ativa o manipulador do tick
register_tick_function("profile");

// Inicializa a função antes de declarar o bloco
profile ();

// Roda um trecho de código, disparando um tick a cada duas instruções
declare (ticks=2) {
    for ($x = 1; $x < 50; ++$x) {
        echo similar_text (md5($x), md5($x*$x)), "<br />;";
    }
}

// Mostra os dados guardados no histórico
print_r (profile (TRUE));
?>
No exemplo acima, o bloco 'declare' grava os tempos a cada segundo nível dentro das instruções no bloco enquanto executam. Esta informação pode ser utilizada para encontrar áreas lentas em segmentos particulares de código. Este processo pode ser realizado de outras formas, mas a utilização de ticks é mais conveniente e fácil de implementar.

Ticks são idealizados para debug, implementação de multitask simples, processos de I/O em background e afins.

Veja também: register_tick_function() e unregister_tick_function().


return

Se chamada em uma função, a instrução return() termina imediatamente a execução da função atual e retorna seu argumento como o valor da função. return() também termina a execução de uma instrução eval() ou de um script.

Se chamada no escopo global, a execução do script atual será terminada. Se o arquivo do script atual foi incluído com include() ou require(), então a execução é devolvida para o arquivo chamador. Especificamente para arquivos de script incluídos com include(), o valor fornecido para return() será devolvido como o valor da chamada include(). Se return() for chamado do arquivo de script principal, então o programa pára. Se o arquivo de script atual é o configurado em auto_prepend_file ou auto_append_file do arquivo de configuração, então a execução desses scripts é finalizada.

Para maiores informações, veja Retornando Valores.

Nota: Note que return() é um construtor de linguagem e não uma função, e parênteses em volta do seus argumentos não são necessários -- de fato, é mais comum não colocá-los que usá-los, sem, entretanto, haver diferença de um jeito ou de outro.


require()

A instrução require() inclui a avalia um arquivo específico.

Informações detalhadas de como essa inclusão funciona está descrita na documentação do include().

require() e include() são idênticos em todas as formas exceto pela manipulação de erros. include() produz um Warning enquanto require() produzirá um Fatal Error. Em outras palavras, não hesite em utilizar require() se na falta de um arquivo quiser parar o processamento da página. include() não se comporta da mesma maneira, e o script poderá continuar nessa situação. Em todo caso, vale a pena confirmar a configuração da diretiva include_path.

Exemplo 12-2. Exemplos de require()s simples

<?php

require 'prepend.php';

require $somefile;

require ('somefile.txt');

?>

Vaja a documentação de include() para mais exemplos.

Nota: Até o PHP 4.0.2, havia o seguinte comportamento: require() mesmo que a linha onde ele está nunca seja executada. É por isso que instruções condicionais não afetam require(). Entretanto, se a linha onde ocorre o require() não for executada, nada do código incluído do arquivo também será. Similarmente, estruturas de loop não afetam o funcionamento do require(). Mas o código incluído pela função será submetida ao loop. A instrução require() apenas ocorre uma vez.

Atenção

Atualmente a versão do PHP para Windows não suporta acesso a arquivos remotos para esta função, mesmo se allow_url_fopen estiver ativado.

Veja também: include(), require_once(), include_once(), eval(), file(), readfile(), virtual() e include_path.


include()

A instrução include() inclui e avalia o arquivo informado.

A documentação a seguir se aplica também a require(). Esses dois construtores são idênticos a exceção de como eles manipulam erros. include() produz um Warning enquanto require() produzirá um Fatal Error. Em outras palavras, utilize require() se você deseja que um arquivo faltando interrompa o processamento da página. include() não se comporta da mesma maneira, permitindo que o script continue nessas situações. Em todo caso, vale a pena confirmar a configuração da diretiva include_path.

Quando um arquivo é incluído, seu código entra no escopo de variável da linha onde a inclusão ocorre. Qualquer variável disponível da linha onde a chamada da inclusão ocorre estará disponível para o arquivo incluído, daquele ponto em diante.

Exemplo 12-3. Exemplos de include()s simples

variaveis.php
<?php

$cor = 'verde';
$fruta = 'maçã';

?>

teste.php
<?php

echo "Uma $fruta $cor"; // Uma

include 'vars.php';

echo "Uma $fruta $cor"; // Uma maçã verde

?>

Se o include ocorre dentro de uma função do arquivo principal, então todo o código incluído será executado como se ele tivesse sido definido dentro daquela função. Da mesma forma, ele seguirá o escopo de variáveis da função.

Exemplo 12-4. Incluindo dentro de funções

<?php

function foo()
{
global $cor;

    include 'variaveis.php';

    echo "Uma $fruta $cor";
}

/* variaveis.php está no escopo de foo(),   *
.* então $fruta NÃO está disponível fora de *
.* seu escopo. $cor estará porque ela foi   *
.* declarada como global                    */

foo();                    // Uma maçã verde
echo "A $fruta $cor";   // Uma maçã

?>

Quando um arquivo é incluído, o interpretador sai do modo PHP e entra no modo HTML (no começo do arquivo incluído), e alterna novamente no seu fim. Por isso, qualquer código dentro do arquivo incluído que precisa ser executado como código PHP tem de ser delimitado por tags válidas de abertura e fechamento.

Se "URL fopen wrappers" estão ativas no PHP (normalmente na configuração default), você pode especificar um arquivo utilizando uma URL (via HTTP) em vez de um caminho local. Se o servidor apontado interpreta o arquivo informado como código PHP, variáveis podem ser passadas ao arquivo incluído na URL de requisição como num HTTP GET. Isto não é necessariamente a mesma coisa que incluir o arquivo e compartilhar o escopo de variável do arquivo principal: o script será executado no servidor remoto e apenas seu resultado será incluído no script local.

Atenção

Atualmente a versão do PHP para Windows não suporta acesso a arquivos remotos para esta função, mesmo se allow_url_fopen estiver ativado.

Exemplo 12-5. include() através de HTTP

<?php

/* Este exemplo assume que www.exemplo.com está configurado para interpretar *
 * arquivos .php mas não .txt. Além, 'Funciona' aqui significa que as        *
 * variáveis $foo e $bar estão disponíveis no arquivo incluído               */

// Não funciona: arquivos txt não são manipulados em www.example.com como PHP
include 'http://www.exemplo.com/arquivo.txt?foo=1&bar=2';

// Não funciona: procura por um arquivo chamado 'arquivo.php?foo=1&bar=2' no
// sistemas de arquivo local.
include 'arquivo.php?foo=1&bar=2';

// Funciona.
include 'http://www.exemplo.com/arquivo.php?foo=1&bar=2';

$foo = 1;
$bar = 2;
include 'arquivo.txt';  // Funciona.
include 'arquivo.php';  // Funciona.

?>
Vaja também: Arquivos Remotos, fopen() e file() para informações relacionadas.

Por serem include() e require() dois construtores de linguagem especiais, você precisa delimitá-los como um bloco de instruções quando utilizados dentro de instruções condicionais.

Exemplo 12-6. include() e instruções condicionais

<?php

// Isto está errado e não funcionará como desejado
if ($condition)
    include $arquivo;
else
    include $outro;


// E este está correto
if ($condition) {
    include $arquivo;
} else {
    include $outro;
}

?>

Também é possível executar uma instrução return() dentro de um arquivo incluído de maneira a finalizar o processamento daquele arquivo e retornar para o script que o chamou. Também é possível retornar valores de arquivos incluídos. Você pode pegar o valor de retorno de um include como faria com uma função normal.

Nota: No PHP 3, o return não pode aparecer dento de um bloco a não ser que ele seja um bloco de função, e nesse caso return() se aplica apenas para a função e não para todo o arquivo.

Exemplo 12-7. Instruções include() e return()

return.php
<?php

$var = 'PHP';

return $var;

?>

noreturn.php
<?php

$var = 'PHP';

?>

testreturns.php
<?php

$foo = include 'return.php';

echo $foo; // imprime 'PHP'

$bar = include 'noreturn.php';

echo $bar; // imprime 1

?>

$bar assimila o valor 1 porque a inclusão foi realizada com sucesso. Verifique a diferença entre os exemplo. O primeiro utiliza return() dentro do arquivo incluído enquanto que o outro não. Há outras maneiras de "incluir" arquivos dentro de variáveis, com fopen(), file() ou utilizando include() através das Funções de Controle de Output.

Veja também: require(), require_once(), include_once(), readfile(), virtual() e include_path.


require_once()

A instrução require_once() incluí e avalia o arquivo especificado durante a execução do script. Seu comportamento é similar ao da instrução require(), a não ser que o arquivo informado já tenha sido incluído, não refazendo a operação novamente. Veja a documentação de require() para maiores informações sobre como essa instrução funciona.

require_once() pode ser utilizado nos casos em que o mesmo arquivo pode acabar sendo incluído mais de uma vez durante a execução de um script em particular, quando na verdade ele só pode ser incluído apenas uma, para evitar problemas com redefinições de funções, alterações nos valores de variáveis, etc.

Para exemplos de utilização de require_once() e include_once(), veja o código do PEAR incluído nas últimas distribuições do código fonte do PHP.

Nota: require_once() foi acrescentado a partir PHP 4.0.1pl2

Atenção

Atualmente a versão do PHP para Windows não suporta acesso a arquivos remotos para esta função, mesmo se allow_url_fopen estiver ativado.

Veja também: require(), include(), include_once(), get_required_files(), get_included_files(), readfile() e virtual().


include_once()

A instrução include_once() inclui e avalia o arquivo especificado durante a execução de um script. Seu comportamento é similar a instrução include(), a não ser que o arquivo informado já tenha sido incluído, não refazendo a operação novamente. Como o nome sugere, ele será incluído apenas uma vez.

include_once() pode ser utilizado nos casos em que o mesmo arquivo pode acabar sendo incluído mais de uma vez durante a execução de um script em particular, quando na verdade ele só pode ser incluído apenas uma para evitar problemas com redefinições de funções, alterações nos valores de variáveis, etc.

Para maiores informações utilizando require_once() e include_once(), veja o código do PEAR incluído nas últimas distribuições do código fonte do PHP.

Nota: include_once() foi acrescentado a partir PHP 4.0.1pl2

Atenção

Atualmente a versão do PHP para Windows não suporta acesso a arquivos remotos para esta função, mesmo se allow_url_fopen estiver ativado.

Veja também: include(), require(), require_once(), get_required_files(), get_included_files(), readfile() e virtual().


Capítulo 13. Funções

Funções definidas pelo usuário

Uma função pode ser definida usando-se a sintaxe como a seguinte:

function foo ($argumento_1, $argumento_2, ..., $argumento_n)
{
    echo "Exemplo de função.\n";
    return $valor_retornado;
}

Qualquer código PHP válido pode aparecer dentro de uma função, mesmo outras funções e definições de classes.

No PHP3, as funções precisam ser definidas antes de serem referenciadas. Esse requisito não existe no PHP4.

O PHP não suporta sobrecarga de funções, e também não é possível cancelar ou alterar a definição de funções previamente declaradas.

O PHP3 não suporta número variável de argumentos para funções, apesar de os argumentos padrões serem suportados (veja Valores padrão de argumentos para mais informações). O PHP4 suporta ambos: veja Número de argumentos variável e as referências das funções func_num_args(), func_get_arg() e func_get_args() para mais informações.


Argumentos de funções

Informações podem ser passadas para funções através da lista de argumentos, que é uma lista de variáveis e/ou constantes delimitados por vírgulas.

O PHP suporta a passagem de argumentos por valor (o padrão), passagem por referência e valores padrão de argumento . Listas de argumentos de comprimento variável são suportadas apenas no PHP4 e posterior; veja Número de argumentos variável e as referências das funções func_num_args(), func_get_arg() e func_get_args() para mais informações. Um efeito similar pode ser atingido no PHP 3 pela passagem de um array de argumentos para uma função:

function takes_array($input)
{
    echo "$input[0] + $input[1] = ", $input[0]+$input[1];
}


Fazendo argumentos serem passados por referência

Por padrão, argumentos de função são passados por valor (de forma que se você mudar o valor do parâmetro dentro da função, ele não é alterado fora da função). Se você deseja permitir que uma função modifique seus argumentos, você precisa passá-los por referência.

Se você quer que um argumento para uma função sempre seja passado por referência, você pode preceder o nome do argumento com um "e comercial" (&) na definição da função:

function add_some_extra(&$string)
{
    $string .= ' e alguma coisa mais.';
}
$str = 'Isto é uma string,';
add_some_extra($str);
echo $str;    // imprime 'Isto é uma string, e alguma coisa mais.'


Valores padrão de argumentos

Uma função pode definir valores padrão no estilo C++ para argumentos escalares, como a seguir:

function cafeteira ($tipo = "cappuccino")
{
    return "Fazendo uma xícara de café $type.\n";
}
echo cafeteira ();
echo cafeteira ("expresso");

A saída do código acima será:
Fazendo uma xícara de café cappucino.
Fazendo uma xícara de café expresso.

O valor padrão precisa ser uma expressão constante, não (por exemplo) uma variável ou um membro de classe.

Note que usando argumentos padrão, qualquer padrão deve vir após os argumentos sem padrão: caso contrário, as coisas não funcionarão como esperado. Considere o seguinte trecho de código:

function iogurtera ($tipo = "azeda", $sabor)
{
    return "Fazendo uma taça de $tipo $sabor.\n";
}

echo iogurtera ("framboesa");   // não funciona como esperado

A saída do exemplo acima é:
Warning: Missing argument 2 in call to iogurtera() in
/usr/local/etc/httpd/htdocs/php3test/functest.html on line 41
Fazendo uma taça de framboesa.

Agora, compare o que está acima com este:

function iogurtera ($sabor, $tipo = "azeda")
{
    return "Fazendo uma taça de $tipo $sabor.\n";
}

echo iogurtera ("framboesa");   // funciona

A saída deste exemplo é:
Fazendo uma taça de framboesa azeda.


Número variável de argumentos

O PHP4 tem suporte para um número variável de argumentos nas funções definidas pelo usuário. Isto é realmente bem fácil, usando as funções func_num_args(), func_get_arg() e func_get_args().

Nenhuma sintaxe especial é requerida, e a lista de argumentos ainda pode ser fornecida explicitamente com as definições de funções e se comportarão normalmente.


Retornando valores

Valores podem ser retornados utilizando a instrução opcional return. Qualquer tipo pode ser retornado, incluindo arrays e objetos. Isto faz com que as função termine sua execução imediatamente e passa o controle de volta para a linha de onde ela foi chamada. Veja a documentação da função return() para maiores informações.

function quadrado ($num)
{
    return $num * $num;
}
echo quadrado (4);   // imprime '16'.

Você não pode retornar múltiplos valores a partir de uma função, mas resultados similares podem ser devolvidos retornando por uma lista.

function numeros_pequenos()
{
    return array (0, 1, 2);
}
list ($zero, $um, $dois) = numeros_pequenos();

Para retornar uma referência de uma função, você precisa utilizar o operador de referência & tanto na declaração da função como quando assimilando o valor retornado para a variável.

function &retorna_referencia()
{
    return $alguma_referencia;
}

$nova_referencia =& retorna_referencia();

Para mais detalhes sobre referências, leia a seção Referências.


old_function

O comando old_function permite que você declare uma função usando uma sintaxe idêntica ao PHP/FI2 (exceto pelo fato de que você precisa substituir 'function' com 'old_function').

Esta é uma implementação obsoleta, e deve ser usada somente pelo conversor PHP/FI2->PHP3.

Atenção

Funções declaradas como old_function não podem ser chamadas a partir de código interno do PHP. Entre outras coisas, isso significa que você não pode usá-las em funções como usort(), array_walk() e register_shutdown_function(). Você pode contornar essa limitação escrevendo um invólucro de função (no formato PHP3 normal) para chamar o old_function.


Funções variáveis

O PHP suporta o conceito de funções variáveis. Isto significa que se um nome de variável tem parênteses no final dela, o PHP procurará uma função com o mesmo nome, qualquer que seja a avaliação da variável, e tentará executá-la. Entre outras coisas, isto pode ser usado para implementar callbacks, tabelas de função e assim por diante.

Funções variáveis não funcionam com construtores de linguagem como echo(), unset(), isset(), empty(), include() ou print().

Exemplo 13-1. Exemplo de funções variáveis

<?php
function foo()
{
    echo "Chamou foo()<br>\n";
}

function bar($arg = '')
{
    echo "Chamou bar(); com argumento '$arg'.<br>\n";
}

$func = 'foo';
$func();
$func = 'bar';
$func('teste');
?>

Veja também: Variáveis variáveis e function_exists().


Capítulo 14. Classes e Objetos

class

Uma classe é uma coleção de variáveis e funções trabalhando com essas variáveis. Uma classe é definida usando-se a seguinte sintaxe:

<?php
class CarrinhoDeCompras
{
    var $items;  // Itens do carrinho de compras

    // Acrescenta a quantidade $num do artigo $artnr no carrinho

    function add_item ($artnr, $num)
    {
        $this->items[$artnr] += $num;
    }

    // Retira a quantidade $num de artigos $artnr do carrinho

    function remove_item ($artnr, $num)
    {
        if ($this->items[$artnr] > $num) {
            $this->items[$artnr] -= $num;
            return true;
        } else {
            return false;
        }
    }
}
?>

Isto define uma classe chamada CarrinhoDeCompras que consiste de uma matriz associativa de artigos no carrinho e duas funções para acrescentar e remover itens deste carrinho.

Cuidado

Os cuidados a seguir devem ser tomados a partir do PHP 4:

O nome stdClass é utilizado internamente pela Zend e é uma palavra reservada. Você não pode ter uma classe chamada stdClass no PHP.

O nome de função __sleep e __wakeup são especialmente mágicos para as classes PHP. Você não pode ter esses nomes em nenhuma de suas classes a não ser que você deseje aplicar essa funcionalidade mágica com elas. Veja abaixo para mais detalhes.

O PHP reserva todos os nomes de funções começando com __ como mágicas. É recomendável que você não utilize nome de funções começando com __ no PHP a não ser que você precise dessas funcionalidades mágicas.

Nota: No PHP 4, somente inicializações com constantes são permitidas para variáveis com var. Para inicializar variáveis com valores não constantes, você precisará de uma função de inicialização chamada automaticamente quando o objeto for construído a partir da classe. Por isso, essa função é conhecida como construtor (veja baixo).

<?php
/* Nenhuma delas funcionarão com o PHP 4 */
class CarrinhoDeCompras
{
    var $data_de_hoje = date("Y-m-d");
    var $nome = $primeiro_nome;
    var $proprietario = 'Fred ' . 'Jones';
    var $items = array("VCR", "TV");
}

/* Esta é a forma como deve ser feito */
class CarrinhoDeCompras
{
    var $data_de_hoje;
    var $nome;
    var $proprietario;
    var $items;

    function CarrinhoDeCompras()
    {
        $this->data_de_hoje = date("Y-m-d");
        $this->nome = $GLOBALS['primeiro_nome'];
        /* etc. . . */
    }
}
?>

Classes são tipos, ou seja, são apenas um modelo das variáveis normais. Você pode criar uma variável (ou instância) do tipo desejado com o operador new.

<?php
$carrinho = new CarrinhoDeCompras;
$carrinho->add_item("10", 1);

$outro_carrinho = new CarrinhoDeCompras;
$outro_carrinho->add_item("0815", 3);

Isto cria os objetos $carrinho e $outro_carrinho, ambos a partir da classe CarrinhoDeCompras. A função add_item() do objeto $carrinho foi chamada e acrescentou 1 item do artigo número 10 a ele. 3 itens do artigo número 0815 foi acrescentado no $outro_carrinho.

Ambos, $carrinho e $outro_carrinho, tem as funções add_item(), remove_item() e a variável itens. Elas são funções e variáveis distintas entre si. Você pode pensar no objetos como os diretórios de um sistema de arquivos. Num disco você pode ter dois arquivos diferentes README.TXT, partindo de que eles estão em diretórios diferentes. Da mesma forma que você teria de especificar o caminho completo para acessar cada arquivo a partir do diretório principal, você também tem de especificar o nome completo do objeto e função que você quer chamar. Em termos do PHP, o diretório principal pode ser o escopo global de nomes, e o separador de diretórios ->. Portanto, os nomes $carrinho->items e $outro_carrinho->items são duas variáveis diferentes. Note que a variável é chamada $carrinho->items e não $carrinho->$items, mesmo porque, um nome de variável em PHP tem apenas um único sinal de cifrão.

// correcto, apenas um $
$carrinho->items = array("10" => 1);

// inválido, porque $carrinho->$items se transforma em $carrinho->""
$carrinho->$items = array("10" => 1);

// correto, mas pode ou não ser o que você quer:
// $carrinho->$myvar se torna $carrinho->items
$myvar = 'items';
$carrinho->$myvar = array("10" => 1);

Quando definindo uma classe, você não pode saber com que nome os objetos serão acessados em seus programas: enquanto escrevia a classe CarrinhoDeCompras, é impossível saber se o objeto criado a partir dela será chamado $carrinho ou $outro_carrinho (ou ainda ambos). Assim, você não pode escrever $carrinho>items dentro da própria classe CarrinhoDeCompras. Entretanto, para poder acessar suas próprias funções e variáveis de dentro da classe, pode-se utilizar a pseudo-variável $this, que pode ser lida como 'eu mesmo' ou 'objeto atual'. Dessa forma, '$this->items[$artnr] += $num' pode ser lido como 'acrescente $num para o contador $artnr do meu array items' ou 'acrescente $num para o contador $artnr do array do objeto atual'.

Nota: Há funções muito boas para manipulação de classes e objetos. Dê uma olhada em Funções de Classes e Objetos


extends

Permite classes com variáveis e funções similares a uma outra classe. De fato, é uma boa prática definir classes genéricas que podem ser utilizadas em todos os seus projetos, e adaptar essas classes com as necessidades específicas de cada projeto. Para facilitar isso, classes podem ser extensões de outras classes. A classe estendida ou derivada tem todas as variáveis e funções da classe base (isto é chamado herança, afora o fato que ninguém morreu) e mais aquelas que venha a acrescentar na versão estendida. Não é possível subtrair uma classe, ou seja, indefinir quaisquer funções ou variáveis existentes. Uma classe estendida é sempre dependente de uma única classe base, e portanto, herança múltipla não é suportada. Classes são estendidas utilizando a instrução 'extends'.

class CarrinhoDeComprasNomeado extends CarrinhoDeCompras
{
    var $proprietario;

    function set_proprietario ($name)
    {
        $this->proprietario = $name;
    }
}

Isto define uma classe chamada CarrinhoDeComprasNomeado que tem todas as variáveis e funções de CarrinhoDeCompras mais a variável $proprietario e uma função set_proprietario(). Você pode criar um carrinho nomeado da maneira usual e configurar e obter o proprietário do carrinho. Você ainda pode continuar utilizando carrinhos normais e carrinhos nomeados:

$ncart = new CarrinhoDeComprasNomeado;
$ncart->set_proprietario("kris");
print $ncart->proprietario;
$ncart->add_item("10", 1);  // (funcionalidade herdade do CarrinhoDeCompras)

Isto é chamado relacionamento "pai-filho". Você cria uma classe pai e utiliza extends para criar uma nova classe baseada na classe pai: sua classe filha. Você ainda pode criar uma nova classe estendida a partir dessa classe filha e assim por diante.

Nota: As classes precisam ser definidas antes de serem utilizadas! Se você estender a classe CarrinhoDeComprasNomeado da classe CarrinhoDeCompras, você precisa antes criar a classe chamada CarrinhoDeCompras. Se você quiser criar uma outra classe chamada CarrinhoDeComprasNomeadoAmarelo baseada na classe CarrinhoDeComprasNomeado você tem que definir CarrinhoDeComprasNomeado primeiro. Trocando em miúdos: a ordem em que as classes são definidas é importante.


Construtores

Cuidado

Os construtores se comportam diferentemente entre o PHP 3 e o PHP 4. A semântica do PHP 4 é atualmente preferencial.

Construtores são funções numa classe que são automaticamente chamados quando você cria uma nova instância da classe com new. No PHP 3, uma função se torna um construtor quando ele tem o mesmo nome da classe. No PHP 4, uma função se torna um construtor quando ele tem o mesmo nome da classe onde ela foi definida (a diferença é sutil, mas crucial --- veja abaixo).

// Funciona no PHP 3 e 4
class CarrinhoDeComprasAutomatico extends CarrinhoDeCompras
{
    function CarrinhoDeComprasAutomatico()
    {
        $this->add_item ("10", 1);
    }
}

Isto define uma classe chamada CarrinhoDeComprasAutomatico que é um CarrinhoDeCompras mais um construtor que inicializa o carrinho com um item do artigo numero "10" cada vez que um novo CarrinhoDeComprasAutomatico for criado com "new". Construtores pode ter argumentos e esses argumentos podem ser opcionais, o que os torna muito mais práticos. Para permitir a criação de classes sem parâmetros, todos os parâmetros dos construtores pode ser feitos opcionais simplesmente por terem valores default.

// Funciona no PHP 3 e 4
class CarrinhoDeComprasComConstrutor extends CarrinhoDeCompras
{
    function CarrinhoDeComprasComConstrutor($item = "10", $num = 1)
    {
        $this->add_item ($item, $num);
    }
}

// Fazendo compras do mesmo modo antigo

$carrinho_default = new CarrinhoDeComprasComConstrutor;

// Fazendo compres de verdade

$carrinhho_diferente = new CarrinhoDeComprasComConstrutor("20", 17);

Você pode usar o operador @ para esconder erros gerados no construtor do objeto, por exemplo: @new.

Cuidado

No PHP 3, classes derivadas e construtores tem uma série de limitações. Os exemplo seguintes precisam ser lidos cuidadosamente para que você entenda essas limitações.

class A
{
    function A()
    {
      echo "Eu sou o construtor de A.<br>\n";
    }
}

class B extends A
{
    function C()
    {
        echo "Eu sou uma função normal.<br>\n";
    }
}

// nenhum construtor é chamado no PHP 3
$b = new B;

No PHP 3, nenhum construtor é chamado no exemplo acima. A regra no PHP 3 é: 'Um construtor é uma função com o mesmo nome da classe'. O nome da classe é B, e não há nenhuma função chamada B() na classe B. Nada ocorre.

Isto foi corrigido no PHP 4 pela instrução de outra regra: Se uma classe não tem construtor, o construtor da classe base é chamado, se existir. O exemplo acima teriam impresso 'Eu sou o construtor de A<br>'. no PHP 4.

class A
{
    function A()
    {
        echo "Eu sou o construtor de A.<br>\n";
    }

    function B()
    {
        echo "Eu sou uma função normal chamada B na classe A.<br>\n";
        echo "Eu não sou o construtor de A.<br>\n";
    }
}

class B extends A
{
    function C()
    {
        echo "Eu sou uma função normal.<br>\n";
    }
}

// Isto irá chamar B() como um construtor.
$b = new B;

No PHP 3, a função B() na classe A se tornará silenciosamente um construtor na classe B, mesmo que isso nunca tenha sido planejado. A regra no PHP 3 é: 'Um construtor é uma função com o mesmo nome da classe'. PHP 3 não se preocupa se a função foi definida na classe B, ou se ela foi herdada.

Isto foi corrigido no PHP 4 pela modificação da regra para: 'Um construtor é uma função com o mesmo nome da classe onde ela é definida.'. Assim, no PHP 4, como a classe B não tem nenhuma função construtora definida nela mesma então o construtor da classe base será chamado, imprimindo 'Eu sou o construtor de A.<br>'.

Cuidado

Nem o PHP 3 ou o 4 chamam automaticamente os construtores da classe base a partir do construtor da classe derivada. É de sua responsabilidade propagar a chamada dos construtores herança acima, onde apropriado.

Nota: Não existem destrutores no PHP 3 ou 4. Mas você pode usar register_shutdown_function() para simular a maioria dos efeitos de destrutores.

Destrutores são funções que são chamadas automaticamente quando um objeto é destruído, ou pela utilização de unset() ou pela simples saída do escopo. Não existem destrutores no PHP.


::

Cuidado

O seguinte é valido para o PHP 4 somente.

As vezes pode ser útil se referir a funções e variáveis na classe base ou referenciar funções em classes que não possuem qualquer instância. O operador :: pode ser utilizado nessas ocasiões.

class A
{
    function exemplo()
    {
        echo "Eu sou a função original A::exemplo().<br>\n";
    }
}

class B extends A
{
    function exemplo()
    {
        echo "Eu sou a função redefinida B::exemplo().<br>\n";
        A::exemplo();
    }
}

// Nao ha nenhum objeto da classe A.
// Isto ira imprimir
//   Eu sou a função original A::exemplo().<br>
A::exemplo();

// cria um objeto a partir da classe B
$b = new B;

// Isto ira imprimir
//   Eu sou a função redefinida B::exemplo().<br>
//   Eu sou a função original A::exemplo().<br>
$b->exemplo();

O exemplo acima chama a função exemplo() da classe A, mas não há nenhum objeto da classe A, então não podemos escrever $a->exemplo() ou qualquer coisa similar, Em vez disso, nós chamamos exemplo() como uma 'função de classe', ou seja, como uma função da classe propriamente dita, não qualquer objeto dessa classe.

Existem funções de classe, mas não variáveis de classe. De fato, não há nenhum objeto durante toda a execução. Assim, uma função de classe não pode usar qualquer variável de objeto (mas pode usar variáveis locais e globais), e nunca podendo utilizar-se de $this.

Ainda no exemplo acima, a classe B redefine a função exemplo(). A definição da função original na classe A é ocultada e nunca disponível, a não ser que você se referencie especificamente a implementação de exemplo() da classe A utilizando-se do operador ::. Escrevemos A::exemplo() para isso (de fato, você também pode escrever parent::exemplo(), como mostrado na próxima seção).

Nesse contexto, existe um objeto e ele pode ter variáveis de objeto. Assim, quando utilizado de DENTRO de uma função de objeto, você pode usar $this e variáveis de objeto.


parent

Você pode se encontrar escrevendo código que precisa referenciar a variáveis e funções na classe base. Isto é particularmente verdade se você derivou uma classe como um refinamento ou especialização de código de sua classe base.

Em vez de utilizar o nome literal da classe base em seu código, você pode usar o nome especial parent, que se refere ao nome da sua classe base como informado na declaração extends. Fazendo isso, evita assim a utilização do nome da sua classe base em mais de um lugar. Se sua árvore de herança mudar durante a implementação, a modificação é mais facilmente realizada pela simples alteração da declaração extends de suas classes.

class A
{
    function exemplo()
    {
        echo "Eu sou A:exemplo() e provenho funcionalidades básicas.<br>\n";
    }
}

class B extends A
{
    function exemplo()
    {
        echo "Eu sou B::exemplo() e provenho funcionalidades adicionais.<br>\n";
        parent::exemplo();
    }
}

$b = new B;

// Isto ira chamar B::exemplo(), que por sua vez chama A::exemplo().
$b->exemplo();


Serializando objetos - objetos em sessões

Nota: No PHP 3, objetos perdem suas associações entre classes através do processo de serialização e desserialização. A variável resultante é do tipo objeto, mas sem classe nem métodos, algo bem sem utilidade (de fato, ele se torna apenas um array com uma sintaxe engraçada).

Cuidado

As informações seguintes se aplicam somente ao PHP 4.

serialize() retorna uma string contendo uma representação linear de qualquer valor que pode ser armazenado no PHP. unserialize() pode ser utilizado para recriar os valores da variável original. Usando serialize para salvar um objeto irá preservar todas as variáveis de um objeto. As funções de um objeto não serão salvas, apenas o nome da classe.

Para ser possível fazer o unserialize() de um objeto, a classe do objeto precisa estar definida. Ou seja, se você tem um objeto $a da classe A em page1.php e a serializa, você consegue uma string que se refere a classe A e contém todos os valores de variáveis contidos em $a. Se você precisa desserializa-la em page2.php, recriando $a da classe A, a definição da classe A precisa estar presente na page2.php. Isto pode por ser feito, por exemplo, armazenando a definição da classe A em um arquivo separado, incluindo este arquivo e ambos os arquivos page1.php e page2.php.

classe_a.inc.php:
  class A
  {
      var $um = 1;

      function mostre_um()
      {
          echo $this->um;
      }
  }

page1.php:
  include("classe_a.inc.php");

  $a = new A;
  $s = serialize($a);
  // armazena $s em algum lugar que page2,php possa encontra-la
  $fp = fopen("armazenamento", "w");
  fputs($fp, $s);
  fclose($fp);

page2.php:
  // Isto e preciso para que unserialize funcione normalmente
  include("classe_a.inc.php");

  $s = implode("", @file("armazenamento"));
  $a = unserialize($s);

  // Agora podemos usar a funcao mostre_um() do objeto $a
  $a->mostre_um();

Se você está utilizando sessões e usar session_register() para registrar objetos, esses objetos serão serializados automaticamente no final de cada script PHP, e automaticamente desserializados em cada uma das páginas seguintes. Isto significa que esse objetos podem estar em qualquer uma de suas páginas desde que elas sejam parte de sua sessão.

É extremamente recomendável que você inclua as definições de classe de todos os objetos registrados de todas as suas páginas, mesmo que você não use essas classes em todas as suas páginas. Se você não o fizer e um objeto for desserializado sem sua definição de classe presente, ele perde usa associação com a classe e se torna um objeto da classe stdClass, sem qualquer funções disponíveis. o que as deixa silenciosamente sem funcionalidade.

Assim, se no exemplo acima $a se tornar parte de uma sessão pela utilização de session_register("a"), você precisa incluir o arquivo classe_a.inc.php em todos as suas páginas, não somente em page1.php e page2.php.


As funções mágicas __sleep e __wakeup

serialize() verifica se sua classe tem uma função com o nome mágico __sleep. Se sim, essa função será executada antes de qualquer serialização. Assim é possível controlar a persistência do objeto enquanto deve retornar um array com os nomes de todas as variáveis daquele objeto que precisam ser serializadas.

__sleep é planejado para fechar quaisquer conexões com bancos de dados que o objeto tenha, realizar commits pendentes ou realizar tarefas de limpeza semelhantes. A função também é útil se você tem objetos muito grandes que não precisam ser salvos completamente.

Da mesma forma, unserialize() verifica pela presença de uma função com o nome mágico __wakeup. Se presente, esta função pode reconstruir quaisquer recursos que o objeto tenha.

__wakeup é planejado para restabelecer conexões com bancos de dados perdidas durante a serialização e para realizar outras tarefas de reinicialização.


Referências dentro do construtor

A criação de referências em construtores pode gerar resultados confusos. Esta seção tentará ajudá-lo e evitar essas situações.

class Foo
{
    function Foo($name)
    {
        // cria uma referencia dentro do array global $globalref
        global $globalref;
        $globalref[] = &$this;
        // configura o nome conforme o parametro
        $this->setName($name);
        // e o mostra
        $this->echoName();
    }

    function echoName()
    {
        echo "<br>",$this->name;
    }

    function setName($name)
    {
        $this->name = $name;
    }
}

Vamos verificar, abaixo, se há alguma diferença entre $bar1, que foi criado usando operador de cópia =, e $bar2 que foi criado usando o operador de referência =& ...

$bar1 = new Foo('configurado no construtor');
$bar1->echoName();
$globalref[0]->echoName();

/* saida:
configurado no construtor
configurado no construtor
configurado no construtor */

$bar2 =& new Foo('configurado no construtor');
$bar2->echoName();
$globalref[1]->echoName();

/* saida:
configurado no construtor
configurado no construtor
configurado no construtor */

Aparentemente não há nenhuma diferença, mas de fato há uma muito significativa: $bar1 e $globalref[0] não se referenciam, elas NÃO são a mesma variável. Isto acontece porque "new" não retorna uma referência por default. Ao invés, retorna uma cópia.

Nota: Isto não causa perda de performance (desde que o PHP 4 usa a contagem de referências) retornando copias em vez de referências. Do contrário, isso oferece melhora por simplificar o trabalho com cópias ao invés de referências, porque a criação de referências toma mais tempo enquanto a criação de cópias virtualmente não toma tempo algum (a não ser no caso de grandes arrays ou objetos, onde um deles é modificado e o(s) outro(s) também na seqüência, então é melhor usar referências para mudar todos ao mesmo tempo).

Para comprovar o que escrevemos acima, analise o seguinte código.

// Agora nos vamos mudar o nome. O que voce espera?
// Voce pode acreditar que ambos $bar1 e $globalref[0] mudem seus nomes...
$bar1->setName('configurado por fora');

// Como mencionado, este nao eh o caso.
$bar1->echoName();
$globalref[0]->echoName();

/* output:
configurado por fora
configurado no construtor */

// Agora vamos ver a diferenca entre $bar2 e $globalref[1]
$bar2->setName('configurado por fora');

// Por sorte, eles nao sao apenas iguais, eles sao a mesma variavel
// Assim, $bar2->name e $globalref[1]->name sao o mesmo tambem
$bar2->echoName();
$globalref[1]->echoName();

/* output:
configurado por fora
configurado por fora */

E apenas mais um exemplo final. Entenda-o com cuidado.

class A
{
    function A($i)
    {
        $this->value = $i;
        // tente entender porque aqui nos nao precisamos de referencia
        $this->b = new B($this);
    }

    function createRef()
    {
        $this->c = new B($this);
    }

    function echoValue()
    {
        echo "<br>","classe ",get_class($this),': ',$this->value;
    }
}


class B
{
    function B(&$a)
    {
        $this->a = &$a;
    }

    function echoValue()
    {
        echo "<br>","classe ",get_class($this),': ',$this->a->value;
    }
}

// Tente entender porque usando uma simples copia aqui ter
// um resultado indesejavel na linha marcada com *
$a =& new A(10);
$a->createRef();

$a->echoValue();
$a->b->echoValue();
$a->c->echoValue();

$a->value = 11;

$a->echoValue();
$a->b->echoValue(); // *
$a->c->echoValue();

/*
output:
classe A: 10
classe B: 10
classe B: 10
classe A: 11
classe B: 11
classe B: 11
*/


Capítulo 15. References Explained

What References Are

References in PHP are a means to access the same variable content by different names. They are not like C pointers, they are symbol table aliases. Note that in PHP, variable name and variable content are different, so the same content can have different names. The most close analogy is with Unix filenames and files - variable names are directory entries, while variable contents is the file itself. References can be thought of as hardlinking in Unix filesystem.


What References Do

PHP references allow you to make two variables to refer to the same content. Meaning, when you do:

$a =& $b

it means that $a and $b point to the same variable.

Nota: $a and $b are completely equal here, that's not $a is pointing to $b or vice versa, that's $a and $b pointing to the same place.

The same syntax can be used with functions, that return references, and with new operator (in PHP 4.0.4 and later):

$bar =& new fooclass();
$foo =& find_var ($bar);

Nota: Not using the & operator causes a copy of the object to be made. If you use $this in the class it will operate on the current instance of the class. The assignment without & will copy the instance (i.e. the object) and $this will operate on the copy, which is not always what is desired. Usually you want to have a single instance to work with, due to performance and memory consumption issues.

While you can use the @ operator to mute any errors in the constructor when using it as @new, this does not work when using the &new statement. This is a limitation of the Zend Engine and will therefore result in a parser error.

The second thing references do is to pass variables by-reference. This is done by making a local variable in a function and a variable in the calling scope reference to the same content. Example:

function foo (&$var)
{
    $var++;
}

$a=5;
foo ($a);

will make $a to be 6. This happens because in the function foo the variable $var refers to the same content as $a. See also more detailed explanations about passing by reference.

The third thing reference can do is return by reference.


What References Are Not

As said before, references aren't pointers. That means, the following construct won't do what you expect:

function foo (&$var)
{
    $var =& $GLOBALS["baz"];
}
foo($bar);

What happens is that $var in foo will be bound with $bar in caller, but then it will be re-bound with $GLOBALS["baz"]. There's no way to bind $bar in the calling scope to something else using the reference mechanism, since $bar is not available in the function foo (it is represented by $var, but $var has only variable contents and not name-to-value binding in the calling symbol table).


Passing by Reference

You can pass variable to function by reference, so that function could modify its arguments. The syntax is as follows:

function foo (&$var)
{
    $var++;
}

$a=5;
foo ($a);
// $a is 6 here

Note that there's no reference sign on function call - only on function definition. Function definition alone is enough to correctly pass the argument by reference.

Following things can be passed by reference:

  • Variable, i.e. foo($a)

  • New statement, i.e. foo(new foobar())

  • Reference, returned from a function, i.e.:

    function &bar()
    {
        $a = 5;
        return $a;
    }
    foo(bar());

    See also explanations about returning by reference.

Any other expression should not be passed by reference, as the result is undefined. For example, the following examples of passing by reference are invalid:

function bar() // Note the missing &
{
    $a = 5;
    return $a;
}
foo(bar());

foo($a = 5) // Expression, not variable
foo(5) // Constant, not variable

These requirements are for PHP 4.0.4 and later.


Returning References

Returning by-reference is useful when you want to use a function to find which variable a reference should be bound to. When returning references, use this syntax:

function &find_var ($param)
{
    ...code...
    return $found_var;
}

$foo =& find_var ($bar);
$foo->x = 2;

In this example, the property of the object returned by the find_var function would be set, not the copy, as it would be without using reference syntax.

Nota: Unlike parameter passing, here you have to use & in both places - to indicate that you return by-reference, not a copy as usual, and to indicate that reference binding, rather than usual assignment, should be done for $foo.


Unsetting References

When you unset the reference, you just break the binding between variable name and variable content. This does not mean that variable content will be destroyed. For example:

$a = 1;
$b =& $a;
unset ($a);

won't unset $b, just $a.

Again, it might be useful to think about this as analogous to Unix unlink call.


Spotting References

Many syntax constructs in PHP are implemented via referencing mechanisms, so everything told above about reference binding also apply to these constructs. Some constructs, like passing and returning by-reference, are mentioned above. Other constructs that use references are:


global References

When you declare variable as global $var you are in fact creating reference to a global variable. That means, this is the same as:

$var =& $GLOBALS["var"];

That means, for example, that unsetting $var won't unset global variable.


$this

In an object method, $this is always reference to the caller object.


Capítulo 16. Error Handling

There are several types of errors and warnings in PHP. They are:

Tabela 16-1. PHP error types

ValueConstantDescriptionNote
1E_ERRORfatal run-time errors 
2E_WARNINGrun-time warnings (non fatal errors) 
4E_PARSEcompile-time parse errors 
8E_NOTICE run-time notices (less serious than warnings)  
16E_CORE_ERRORfatal errors that occur during PHP's initial startupPHP 4 only
32E_CORE_WARNING warnings (non fatal errors) that occur during PHP's initial startup PHP 4 only
64E_COMPILE_ERRORfatal compile-time errorsPHP 4 only
128E_COMPILE_WARNINGcompile-time warnings (non fatal errors)PHP 4 only
256E_USER_ERRORuser-generated error messagePHP 4 only
512E_USER_WARNINGuser-generated warning messagePHP 4 only
1024E_USER_NOTICE user-generated notice messagePHP 4 only
 E_ALLall of the above, as supported 

The above values (either numerical or symbolic) are used to build up a bitmask that specifies which errors to report. You can use the bitwise operators to combine these values or mask out certain types of errors. Note that only '|', '~', '!', and '&' will be understood within php.ini, however, and that no bitwise operators will be understood within php3.ini.

In PHP 4, the default error_reporting setting is E_ALL & ~E_NOTICE, meaning to display all errors and warnings which are not E_NOTICE-level. In PHP 3, the default setting is (E_ERROR | E_WARNING | E_PARSE), meaning the same thing. Note, however, that since constants are not supported in PHP 3's php3.ini, the error_reporting setting there must be numeric; hence, it is 7.

The initial setting can be changed in the ini file with the error_reporting directive, in your Apache httpd.conf file with the php_error_reporting (php3_error_reporting for PHP 3) directive, and lastly it may be set at runtime within a script by using the error_reporting() function.

Atenção

When upgrading code or servers from PHP 3 to PHP 4 you should check these settings and calls to error_reporting() or you might disable reporting the new error types, especially E_COMPILE_ERROR. This may lead to empty documents without any feedback of what happened or where to look for the problem.

All PHP expressions can also be called with the "@" prefix, which turns off error reporting for that particular expression. If an error occurred during such an expression and the track_errors feature is enabled, you can find the error message in the global variable $php_errormsg.

Nota: The @ error-control operator prefix will not disable messages that are the result of parse errors.

Atenção

Currently the @ error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use @ to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.

Below we can see an example of using the error handling capabilities in PHP. We define a error handling function which logs the information into a file (using an XML format), and e-mails the developer in case a critical error in the logic happens.

Exemplo 16-1. Using error handling in a script

<?php
// we will do our own error handling
error_reporting(0);

// user defined error handling function
function userErrorHandler ($errno, $errmsg, $filename, $linenum, $vars) {
    // timestamp for the error entry
    $dt = date("Y-m-d H:i:s (T)");

    // define an assoc array of error string
    // in reality the only entries we should
    // consider are 2,8,256,512 and 1024
    $errortype = array (
                1   =>  "Error",
                2   =>  "Warning",
                4   =>  "Parsing Error",
                8   =>  "Notice",
                16  =>  "Core Error",
                32  =>  "Core Warning",
                64  =>  "Compile Error",
                128 =>  "Compile Warning",
                256 =>  "User Error",
                512 =>  "User Warning",
                1024=>  "User Notice"
                );
    // set of errors for which a var trace will be saved
    $user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
    
    $err = "<errorentry>\n";
    $err .= "\t<datetime>".$dt."</datetime>\n";
    $err .= "\t<errornum>".$errno."</errornum>\n";
    $err .= "\t<errortype>".$errortype[$errno]."</errortype>\n";
    $err .= "\t<errormsg>".$errmsg."</errormsg>\n";
    $err .= "\t<scriptname>".$filename."</scriptname>\n";
    $err .= "\t<scriptlinenum>".$linenum."</scriptlinenum>\n";

    if (in_array($errno, $user_errors))
        $err .= "\t<vartrace>".wddx_serialize_value($vars,"Variables")."</vartrace>\n";
    $err .= "</errorentry>\n\n";
    
    // for testin