Sophie

Sophie

distrib > Mandriva > 2008.1 > i586 > by-pkgid > 703d980c580707c382b4e43e25965bc5 > files > 11784

php-manual-pt_BR-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Funções</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="Manual do PHP"
HREF="index.html"><LINK
REL="UP"
TITLE="Referência da Linguagem"
HREF="langref.html"><LINK
REL="PREVIOUS"
TITLE="include_once"
HREF="function.include-once.html"><LINK
REL="NEXT"
TITLE="Argumentos de funções"
HREF="functions.arguments.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></HEAD
><BODY
CLASS="chapter"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Manual do PHP</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.include-once.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="functions.arguments.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="chapter"
><H1
><A
NAME="language.functions"
>Capítulo 17. Funções</A
></H1
><DIV
CLASS="TOC"
><DL
><DT
><B
>Índice</B
></DT
><DT
><A
HREF="language.functions.html#functions.user-defined"
>Funções definidas pelo usuário</A
></DT
><DT
><A
HREF="functions.arguments.html"
>Argumentos de funções</A
></DT
><DT
><A
HREF="functions.returning-values.html"
>Retornando valores</A
></DT
><DT
><A
HREF="functions.variable-functions.html"
>Funções variáveis</A
></DT
><DT
><A
HREF="functions.internal.html"
>Funções internas (built-in)</A
></DT
></DL
></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="functions.user-defined"
>Funções definidas pelo usuário</A
></H1
><P
>&#13;    Uma função pode ser definida usando-se a sintaxe como a seguinte:
   </P
><P
>&#13;    <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5162"
></A
><P
><B
>Exemplo 17-1. Psuedo-código que de demonstração de uma função</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">function </font><font color="#0000BB">foo </font><font color="#007700">(</font><font color="#0000BB">$arg_1</font><font color="#007700">, </font><font color="#0000BB">$arg_2</font><font color="#007700">, </font><font color="#FF8000">/* ..., */ </font><font color="#0000BB">$arg_n</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Exemplo de função.\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">$valor_retornado</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
   </P
><P
>&#13;    Qualquer código PHP válido pode aparecer dentro de uma função, mesmo outras funções
    e definições de <A
HREF="language.oop.html#keyword.class"
>classes</A
>.

   </P
><P
>&#13;    Nomes de funções seguem as mesmas regras que outros rótulo no PHP. Um
    nome de função válido começa com uma letra ou um sublinhado, seguido,
    seguido por qualquer número de letras, números ou sublinhado. Como uma expressão
    regular, seria expressado com:
    <TT
CLASS="literal"
>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*</TT
>.
   </P
><DIV
CLASS="tip"
><BLOCKQUOTE
CLASS="tip"
><P
><B
>Dica: </B
>Você também pode querer dar
  uma ohada em <A
HREF="userlandnaming.html"
>Apêndice S</A
>.</P
></BLOCKQUOTE
></DIV
><P
>&#13;    No PHP 3, as funções precisam ser definidas antes de serem referenciadas. Esse
    requisito não existe desde PHP 4. <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>Exceto</I
></SPAN
> quando
    uma função é definida condicionalmente como mostrada nos dois exemplos
    abaixo:
   </P
><P
>&#13;    Quando uma função é definida condicionalmente como nos dois
    exemplos abaixo, sua definição precusa ser processada <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>antes</I
></SPAN
>
    de ser chamada.
   </P
><P
>&#13;    <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5177"
></A
><P
><B
>Exemplo 17-2. Funções definidas condicionalmente</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /><br />$makefoo </font><font color="#007700">= </font><font color="#0000BB">true</font><font color="#007700">;<br /><br /></font><font color="#FF8000">/* Nos nao podemos chamar foo() daqui<br />&nbsp;&nbsp;&nbsp;porque ela ainda não existe,<br />&nbsp;&nbsp;&nbsp;mas nos podemos chamar bar() */<br /><br /></font><font color="#0000BB">bar</font><font color="#007700">();<br /><br />if (</font><font color="#0000BB">$makefoo</font><font color="#007700">) {<br />&nbsp;&nbsp;function </font><font color="#0000BB">foo </font><font color="#007700">()<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Eu não existo até que o programa passe por aqui.\n"</font><font color="#007700">;<br />&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#FF8000">/* Agora nos podemos chamar foo()<br />&nbsp;&nbsp;&nbsp;porque $makefoo foi avaliado como true */<br /><br /></font><font color="#007700">if (</font><font color="#0000BB">$makefoo</font><font color="#007700">) </font><font color="#0000BB">foo</font><font color="#007700">();<br /><br />function </font><font color="#0000BB">bar</font><font color="#007700">()<br />{<br />&nbsp;&nbsp;echo </font><font color="#DD0000">"Eu existo imediatamente desde o programa começar.\n"</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
   </P
><P
>&#13;    <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5181"
></A
><P
><B
>Exemplo 17-3. Funções dentro de funções</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">function </font><font color="#0000BB">foo</font><font color="#007700">()<br />{<br />&nbsp;&nbsp;function </font><font color="#0000BB">bar</font><font color="#007700">()<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Eu não existo até foo() ser chamada.\n"</font><font color="#007700">;<br />&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#FF8000">/* Nós não podemos chamar bar() ainda<br />&nbsp;&nbsp;&nbsp;porque ela ainda não foi definida. */<br /><br /></font><font color="#0000BB">foo</font><font color="#007700">();<br /><br /></font><font color="#FF8000">/* Agora nós podemos chamar bar(),<br />&nbsp;&nbsp;&nbsp;porque o processamento de foo()<br />&nbsp;&nbsp;&nbsp;tornou a primeira acessivel */<br /><br /></font><font color="#0000BB">bar</font><font color="#007700">();<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
   </P
><P
>&#13;    Todas as funções e classes no PHP tem escopo global - elas podem
    ser chamadas fora de uma função mesmo que tenham sido definidas dentro e vice versa.
   </P
><P
>&#13;    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.
   </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>
     Nomes de funções são insensíveis ao caso, mas é melhor chamar
     as funções da mesma forma que ela aparecem nas declarações.
    </P
></BLOCKQUOTE
></DIV
><P
>&#13;    O PHP3 não suporta número variável de argumentos para funções, apesar de os
    argumentos padrões serem suportados (veja <A
HREF="functions.arguments.html#functions.arguments.default"
>Valores padrão de argumentos
    </A
> para mais informações). Ambos são suportados a partir do PHP4: veja <A
HREF="functions.arguments.html#functions.variable-arg-list"
>Número de argumentos
    variável</A
> e as referências das funções
    <A
HREF="function.func-num-args.html"
><B
CLASS="function"
>func_num_args()</B
></A
>,
    <A
HREF="function.func-get-arg.html"
><B
CLASS="function"
>func_get_arg()</B
></A
> e
    <A
HREF="function.func-get-args.html"
><B
CLASS="function"
>func_get_args()</B
></A
> para mais informações.
   </P
><P
>&#13;    É possível chamar funções recursivas no PHP. Entretanto evite o
    uso de funções/métodos recursivos com mais de 100-200 níveis de recursão já que isso
    pode estourar a pilha e causar o encerramento do script atual.
    <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5195"
></A
><P
><B
>Exemplo 17-4. Funções Recursivas</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">function </font><font color="#0000BB">recursion</font><font color="#007700">(</font><font color="#0000BB">$a</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">$a </font><font color="#007700">&lt; </font><font color="#0000BB">20</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"$a</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">recursion</font><font color="#007700">(</font><font color="#0000BB">$a </font><font color="#007700">+ </font><font color="#0000BB">1</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
   </P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.include-once.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Principal</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="functions.arguments.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.include-once.html"
><B
CLASS="function"
>include_once()</B
></A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="langref.html"
ACCESSKEY="U"
>Acima</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Argumentos de funções</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>