<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Estruturas de Controle</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="Operadores de Arrays" HREF="language.operators.array.html"><LINK REL="NEXT" TITLE="else" HREF="control-structures.else.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="language.operators.array.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="control-structures.else.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="chapter" ><H1 ><A NAME="language.control-structures" >Capítulo 16. Estruturas de Controle</A ></H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Índice</B ></DT ><DT ><A HREF="language.control-structures.html#control-structures.if" ><TT CLASS="literal" >if</TT ></A ></DT ><DT ><A HREF="control-structures.else.html" ><TT CLASS="literal" >else</TT ></A ></DT ><DT ><A HREF="control-structures.elseif.html" ><TT CLASS="literal" >elseif</TT ></A ></DT ><DT ><A HREF="control-structures.alternative-syntax.html" >Sintaxe alternativa para estruturas de controle</A ></DT ><DT ><A HREF="control-structures.while.html" ><TT CLASS="literal" >while</TT ></A ></DT ><DT ><A HREF="control-structures.do.while.html" ><TT CLASS="literal" >do-while</TT ></A ></DT ><DT ><A HREF="control-structures.for.html" ><TT CLASS="literal" >for</TT ></A ></DT ><DT ><A HREF="control-structures.foreach.html" ><TT CLASS="literal" >foreach</TT ></A ></DT ><DT ><A HREF="control-structures.break.html" ><TT CLASS="literal" >break</TT ></A ></DT ><DT ><A HREF="control-structures.continue.html" ><TT CLASS="literal" >continue</TT ></A ></DT ><DT ><A HREF="control-structures.switch.html" ><TT CLASS="literal" >switch</TT ></A ></DT ><DT ><A HREF="control-structures.declare.html" ><TT CLASS="literal" >declare</TT ></A ></DT ><DT ><A HREF="function.return.html" >return</A ></DT ><DT ><A HREF="function.require.html" ><A HREF="function.require.html" ><B CLASS="function" >require()</B ></A ></A ></DT ><DT ><A HREF="function.include.html" ><A HREF="function.include.html" ><B CLASS="function" >include()</B ></A ></A ></DT ><DT ><A HREF="function.require-once.html" ><A HREF="function.require-once.html" ><B CLASS="function" >require_once()</B ></A ></A ></DT ><DT ><A HREF="function.include-once.html" ><A HREF="function.include-once.html" ><B CLASS="function" >include_once()</B ></A ></A ></DT ></DL ></DIV ><P > 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. </P ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="control-structures.if" ><TT CLASS="literal" >if</TT ></A ></H1 ><P > A construção <TT CLASS="literal" >if</TT > é 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 <TT CLASS="literal" >if</TT > que é similar àquela do C: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4520" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >if (expressao) instrucoes</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > Como descrita na <A HREF="language.expressions.html" >seção sobre expressões </A >, <TT CLASS="replaceable" ><I >expressao</I ></TT > é avaliado por seu contexto Booleano. Se <TT CLASS="replaceable" ><I >expressao</I ></TT > for avaliado como <TT CLASS="constant" ><B >TRUE</B ></TT >, o PHP executará <TT CLASS="replaceable" ><I >instrucoes</I ></TT >, e se for avaliado como <TT CLASS="constant" ><B >FALSE</B ></TT >, ele será ignorado. Maiores informações sobre a avaliação para <TT CLASS="constant" ><B >FALSE</B ></TT > podem ser encontradas na seção <A HREF="language.types.boolean.html#language.types.boolean.casting" >Convertendo para Booleanos</A > . </P ><P > Os exemplos a seguir mostrariam que <SAMP CLASS="computeroutput" >a é maior que b</SAMP > se <CODE CLASS="varname" >$a</CODE > for maior que <CODE CLASS="varname" >$b</CODE >: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4535" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">if (</font><font color="#0000BB">$a </font><font color="#007700">> </font><font color="#0000BB">$b</font><font color="#007700">)<br /> echo </font><font color="#DD0000">"a é maior que b"</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > 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 <TT CLASS="literal" >if</TT >. Em vez disso, você pode colocar várias instruções em um agrupamento de comandos. Por exemplo, este código mostraria <SAMP CLASS="computeroutput" >a é maior que b</SAMP > se <CODE CLASS="varname" >$a</CODE > for maior que <CODE CLASS="varname" >$b</CODE >, e então atribuiria o valor de <CODE CLASS="varname" >$a</CODE > para <CODE CLASS="varname" >$b</CODE >: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4544" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">if (</font><font color="#0000BB">$a </font><font color="#007700">> </font><font color="#0000BB">$b</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"a é maior que b"</font><font color="#007700">;<br /> </font><font color="#0000BB">$b </font><font color="#007700">= </font><font color="#0000BB">$a</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > Comandos <TT CLASS="literal" >if</TT > podem ser aninhados indefinidamente dentro de outros comandos <TT CLASS="literal" >if</TT >, o que faz com que você complete a flexibilidade para a execução condicional de várias partes do seu programa. </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="language.operators.array.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="control-structures.else.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Operadores de Arrays</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="langref.html" ACCESSKEY="U" >Acima</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><TT CLASS="literal" >else</TT ></TD ></TR ></TABLE ></DIV ></BODY ></HTML >