<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Inteiros</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="Tipos" HREF="language.types.html"><LINK REL="PREVIOUS" TITLE="Booleanos" HREF="language.types.boolean.html"><LINK REL="NEXT" TITLE="Números de ponto flutuante" HREF="language.types.float.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="sect1" 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.types.boolean.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Capítulo 11. Tipos</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.types.float.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.types.integer" >Inteiros</A ></H1 ><P > Um inteiro é um número do conjunto Z = {..., -2, -1, 0, 1, 2, ...}. </P ><P > Veja também: <A HREF="ref.gmp.html" >Inteiros de tamanho arbitrário / GMP</A >, <A HREF="language.types.float.html" >Números de ponto flutuante</A > e <A HREF="ref.bc.html" >Precisão arbitrária / BCMath</A >. </P ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.types.integer.syntax" >Sintaxe</A ></H2 ><P > Inteiros podem ser especificados em notação decimal (base 10), hexadecimal (base 16) ou octal (base 8), opcionalmente precedido de sinal (- ou +). </P ><P > Para usar a notação octal, você precisa preceder o número com um <TT CLASS="literal" >0</TT > (zero). Para utilizar a notação hexadecimal, preceda número com <TT CLASS="literal" >0x</TT >. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2911" ></A ><P ><B >Exemplo 11-1. Literais inteiras</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$a </font><font color="#007700">= </font><font color="#0000BB">1234</font><font color="#007700">; </font><font color="#FF8000"># número decimal<br /></font><font color="#0000BB">$a </font><font color="#007700">= -</font><font color="#0000BB">123</font><font color="#007700">; </font><font color="#FF8000"># um número negativo<br /></font><font color="#0000BB">$a </font><font color="#007700">= </font><font color="#0000BB">0123</font><font color="#007700">; </font><font color="#FF8000"># número octal (equivalente a 83 em decimal)<br /></font><font color="#0000BB">$a </font><font color="#007700">= </font><font color="#0000BB">0x1A</font><font color="#007700">; </font><font color="#FF8000"># número hexadecimal (equivalente a 26 em decimal)<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Formalmente, as possíveis representação de inteiros são: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2914" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >decimal : [1-9][0-9]* | 0 hexadecimal : 0[xX][0-9a-fA-F]+ octal : 0[0-7]+ integer : [+-]?decimal | [+-]?hexadecimal | [+-]?octal</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > O tamanho de um inteiro é dependente de plataforma, sendo um numero aproximado a 2 bilhões o valor mais comum (número de 32 bits com sinal). O PHP não suporta inteiros sem sinal. </P ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.types.integer.overflow" >Overflow de inteiros</A ></H2 ><P > Se você especifica um número além dos limites do tipo inteiro, ele será interpretado como um ponto flutuante. Assim, se você realizar uma operação que resulte em um número além dos limites do tipo inteiro, um ponto flutuante será retornado também. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2923" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">$ </font><font color="#0000BB">numero_grande </font><font color="#007700">= </font><font color="#0000BB">2147483647</font><font color="#007700">;<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$numero_grande</font><font color="#007700">);<br /></font><font color="#FF8000">// saida: int(2147483647)<br /><br /></font><font color="#0000BB">$numero_grande </font><font color="#007700">= </font><font color="#0000BB">2147483648</font><font color="#007700">;<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$numero_grande</font><font color="#007700">);<br /></font><font color="#FF8000">// saida: float(2147483648)<br /><br />// isto também é permitido para especificação de números hexadecimais:<br /></font><font color="#0000BB">var_dump</font><font color="#007700">( </font><font color="#0000BB">0x80000000 </font><font color="#007700">);<br /></font><font color="#FF8000">// saida: float(2147483648)<br /><br /></font><font color="#0000BB">$milhao </font><font color="#007700">= </font><font color="#0000BB">1000000</font><font color="#007700">;<br /></font><font color="#0000BB">$numero_grande </font><font color="#007700">= </font><font color="#0000BB">50000 </font><font color="#007700">* </font><font color="#0000BB">$milhao</font><font color="#007700">;<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$numero_grande</font><font color="#007700">);<br /></font><font color="#FF8000">// saida: float(50000000000)<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > <DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Atenção</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > Infelizmente, há um bug no PHP que faz que ele nem sempre trabalhe corretamente quando há números negativos envolvidos. Por exemplo, quando você faz <TT CLASS="literal" >-50000 * $milhao</TT >, o resultado será <TT CLASS="literal" >-429496728</TT >. Entretanto, quando ambos os operadores são positivos, isso não ocorre. </P ><P > Isto foi resolvido no PHP 4.1.0. </P ></TD ></TR ></TABLE ></DIV > </P ><P > Não há operador de divisão inteira no PHP. <TT CLASS="literal" >1/2</TT > retorna o ponto flutuante <TT CLASS="literal" >0.5</TT >. Você pode moldar (cast) o valor para inteiro para sempre truncar o número, ou você pode usar a função <A HREF="function.round.html" ><B CLASS="function" >round()</B ></A >. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2935" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />var_dump</font><font color="#007700">(</font><font color="#0000BB">25</font><font color="#007700">/</font><font color="#0000BB">7</font><font color="#007700">); </font><font color="#FF8000">// float(3.5714285714286)<br /></font><font color="#0000BB">var_dump</font><font color="#007700">((int) (</font><font color="#0000BB">25</font><font color="#007700">/</font><font color="#0000BB">7</font><font color="#007700">)); </font><font color="#FF8000">// int(3)<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">round</font><font color="#007700">(</font><font color="#0000BB">25</font><font color="#007700">/</font><font color="#0000BB">7</font><font color="#007700">)); </font><font color="#FF8000">// float(4) <br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.types.integer.casting" >Convertendo para inteiro</A ></H2 ><P > Para converter explicitamente um valor para inteiro, utilize-se dos modificadores <TT CLASS="literal" >(int)</TT > ou <TT CLASS="literal" >(integer)</TT >. Entretanto, na maioria dos casos, você não precisa utilizar o modificador, desde que qualquer valor será automaticamente convertido se um operador, função ou estrutura de controle requerer um argumento inteiro. Você também pode converter o valor de um inteiro com a função <A HREF="function.intval.html" ><B CLASS="function" >intval()</B ></A >. </P ><P > Veja também <A HREF="language.types.type-juggling.html" >Manipulação de tipos</A >. </P ><DIV CLASS="sect3" ><H3 CLASS="sect3" ><A NAME="language.types.integer.casting.from-boolean" >de <A HREF="language.types.boolean.html" >booleanos</A ></A ></H3 ><P > <TT CLASS="constant" ><B >FALSE</B ></TT > será retornado como <TT CLASS="literal" >0</TT > (zero), e <TT CLASS="constant" ><B >TRUE</B ></TT > como <TT CLASS="literal" >1</TT > (um). </P ></DIV ><DIV CLASS="sect3" ><H3 CLASS="sect3" ><A NAME="language.types.integer.casting.from-float" >De <A HREF="language.types.float.html" >números de ponto flutuante</A ></A ></H3 ><P > Quando convertendo de números de ponto flutuante para inteiros, o número será <SPAN CLASS="emphasis" ><I CLASS="emphasis" >truncado</I ></SPAN >. </P ><P > Se o número convertido estiver além dos limites de um inteiro (usualmente <TT CLASS="literal" >+/- 2.15e+9 = 2^31</TT >), o resultado é indefinido, mesmo porque o ponto flutuante não tem a mesma precisão para fornecer um resultado inteiro exato. Não se preocupe, pois nenhum aviso será emitido neste caso! </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Atenção</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > Nunca modifique uma fração desconhecida para inteiro, porque isto pode fornecer resultados inesperados as vezes. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2965" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">echo (int) ( (</font><font color="#0000BB">0.1</font><font color="#007700">+</font><font color="#0000BB">0.7</font><font color="#007700">) * </font><font color="#0000BB">10 </font><font color="#007700">); </font><font color="#FF8000">// imprime 7!<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > Para maiores informações, veja o <A HREF="language.types.float.html#warn.float-precision" >alerta sobre a precisão de número flutuante.</A >. </P ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="sect3" ><H3 CLASS="sect3" ><A NAME="language.types.integer.casting.from-string" >De strings</A ></H3 ><P > Veja <A HREF="language.types.string.html#language.types.string.conversion" >Conversão de strings para números</A > </P ></DIV ><DIV CLASS="sect3" ><H3 CLASS="sect3" ><A NAME="language.types.integer.casting.from-other" >De outros tipos</A ></H3 ><P > <DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Cuidado</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > O comportamento da conversão de um inteiro é indefinido de outros tipos. Atualmente, o comportamento é o mesmo como se primeiro o valor fosse <A HREF="language.types.boolean.html#language.types.boolean.casting" >convertido para booleano</A >. Entretanto, <SPAN CLASS="emphasis" ><I CLASS="emphasis" >não confie</I ></SPAN > neste comportamento, pois ele pode mudar sem aviso. </P ></TD ></TR ></TABLE ></DIV > </P ></DIV ></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.types.boolean.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="language.types.float.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Booleanos</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.types.html" ACCESSKEY="U" >Acima</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Números de ponto flutuante</TD ></TR ></TABLE ></DIV ></BODY ></HTML >