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

search for in the

Booleanos> <Comentarios
Last updated: Fri, 04 Jul 2008

view this page in

Tipos

Table of Contents

Introducción

PHP soporta ocho tipos primitivos.

Cuatro tipos escalares:

Dos tipos compuestos:

Y finalmente dos tipos especiales:

Este manual introduce también algunos pseudo-tipos por razones de legibilidad:

Y la pseudo-variable $... .

Puede que algunas referencias al tipo "double" aun existan en el manual. Considere al tipo double como el mismo que float; los dos nombres existen sólo por razones históricas.

El tipo de una variable usualmente no es declarado por el programador; en cambio, es decidido en tiempo de ejecución por PHP dependiendo del contexto en el que es usado la variable.

Note: Si desea chequear el tipo y valor de una expresión, use la función var_dump(). Para obtener una representación legible para humanos del tipo para propósitos de depuración, use la función gettype(). Para chequear por un cierto tipo, no use gettype(), si no las funciones is_tipo. Algunos ejemplos:

<?php
$un_bool 
TRUE;   // un valor booleano
$un_str  "foo";  // una cadena
$un_str2 'foo';  // una cadena
$un_int  12;     // un entero

echo gettype($un_bool); // imprime: boolean
echo gettype($un_str);  // imprime: string

// Si este valor es un entero, incrementarlo en cuatro
if (is_int($un_int)) {
    
$un_int += 4;
}

// Si $bool es una cadena, imprimirla
// (no imprime nada)
if (is_string($un_bool)) {
    echo 
"Cadena: $un_bool";
}
?>

Para forzar la conversión de una variable a cierto tipo, puede moldear la variable o usar la función settype() sobre ella.

Note que una variable puede ser evaluada con valores diferentes en ciertas situaciones, dependiendo del tipo que posee en cada momento. Para más información, vea la sección sobre Manipulación de Tipos. Las tablas de comparación de tipos pueden resultar útiles también, ya que muestran ejemplos de varias comparaciones relacionadas con tipos.



Booleanos> <Comentarios
Last updated: Fri, 04 Jul 2008
 
add a note add a note User Contributed Notes
Tipos
jonah_whalehosting_ca
12-Apr-2007 10: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 12:32
Note that you can chain type castng:

var_dump((string)(int)false); //string(1) "0"
shahnaz khan
18-Mar-2005 04: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 06: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> <Comentarios
Last updated: Fri, 04 Jul 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites