PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Booleanos> <Comentários
Last updated: Fri, 22 Aug 2008

view this page in

Tipos

Índice

Introdução

O PHP suporta os oitos tipos primitivos.

São quatros tipos básicos:

Dois tipos compostos:

E finalmente dois tipos especiais:

Este manual também introduz alguns pseudo-tipos por razões de legibilidade:

E a pseudo-variável $... .

Algumas referências para o tipo "double" podem aparecer no manual. Considere o tipo double como sendo o float; os dois nomes existem por razões históricas.

O tipo de uma variável geralmente não é definido pelo programador: isto é decidido em tempo de execução pelo PHP, dependendo do contexto no qual a variável é usada.

Nota: Para checar o tipo e valor de uma expressão, utilize a função var_dump(). Para ter uma representação legível de um tipo para debugar, use a função gettype(). Para verificar por um certo tipo, não use gettype(), mas sim as funções is_tipo. Vejamos alguns exemplos:

<?php
$a_bool 
TRUE;   // um booleano
$a_str  "foo";  // uma string
$a_str2 'foo';  // uma string
$an_int 12;     // um inteiro

echo gettype($a_bool); // mostra:  boolean
echo gettype($a_str);  // mostra:  string

// Se ele é um inteiro, incrementa-o com quatro
if (is_int($an_int)) {
    
$an_int += 4;
}

// Se $bool é uma string, mostre-a
// (não imprime nada)
if (is_string($a_bool)) {
    echo 
"String: $a_bool";
}
?>

Para forçar a conversão de uma variável para um certo tipo, você pode converter (casting) a variável ou usar a função settype() nela.

Note que uma variável pode ser avaliada com valores diferentes em certas situações, dependendo de qual tipo ela é no momento. Para mais informações, veja a seção Manipulação de tipos. A tabela de conversão de tipos também pode ser útil, como mostra exemplos de comparações de vários tipos.



Booleanos> <Comentários
Last updated: Fri, 22 Aug 2008
 
add a note add a note User Contributed Notes
Tipos
jonah_whalehosting_ca
12-Apr-2007 01:33
In reply to Philip, form data could also be an array.  so there are two types you can expect from $_REQUEST (and it's associates): string and array.
arjini at gmail dot com
06-Dec-2005 03:32
Note that you can chain type castng:

var_dump((string)(int)false); //string(1) "0"
shahnaz khan
18-Mar-2005 07:40
if we use gettype() before initializinf any variable it give NULL
for eg.

<?php
$foo
;
echo
gettype($foo);
?>

it will show

NULL
Trizor of www.freedom-uplink.org
29-Jun-2004 09:14
The differance of float and double dates back to a FORTRAN standard. In FORTRAN Variables aren't as loosly written as in PHP and you had to define variable types(OH NOES!). FLOAT or REAL*4 (For all you VAX people out there) defined the variable as a standard precision floating point, with 4 bytes of memory allocated to it. DOUBLE PRECISION or REAL*8 (Again for the VAX) was identical to FLOAT or REAL*4, but with an 8 byte allocation of memory instead of a 4 byte allocation.

In fact most modern variable types date back to FORTRAN, except a string was called a CHARACHTER*N and you had to specify the length, or CHARACHTER*(*) for a variable length string. Boolean was LOGICAL, and there weren't yet objects, and there was support for complex numbers(a+bi).

Of course, most people reading this are web programmers and could care less about the mathematical background of programming.

NOTE: Object support was added to FORTRAN in the FORTRAN90 spec, and expanded with the FORTRAN94 spec, but by then C was the powerful force on the block, and most people who still use FORTRAN use the FORTRAN77.

Booleanos> <Comentários
Last updated: Fri, 22 Aug 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites