mirror of
https://github.com/php/doc-es.git
synced 2026-04-29 01:53:12 +02:00
87b8b0cc29
git-svn-id: https://svn.php.net/repository/phpdoc/es/trunk@334382 c90b9560-bf6c-de11-be94-00142212c4b1
289 lines
7.8 KiB
XML
289 lines
7.8 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: eda3b067eb85fcd307682aa8966d3c017cc29be2 Maintainer: gerardocdc Status: ready -->
|
|
<!-- Reviewed: no -->
|
|
<sect1 xml:id="language.types.type-juggling">
|
|
<title>Manipulación de tipos</title>
|
|
|
|
<simpara>
|
|
PHP no requiere (ni soporta) la definición explicita de tipos en la declaración
|
|
de variables; el tipo de la variable se determina por el contexto en el cual
|
|
se emplea la variable. Es decir, si se asigna un valor <type>string</type> a una
|
|
variable <varname>$var</varname>, entonces <varname>$var</varname> se convierte en un
|
|
<type>string</type>. Si un valor <type>integer</type> es entonces asignado a la misma
|
|
variable <varname>$var</varname>, ésta se convierte en <type>integer</type>.
|
|
</simpara>
|
|
|
|
<para>
|
|
Un ejemplo de la conversión de tipos automática de PHP es el operador suma '+'.
|
|
Si ambos operandos son <type>float</type>, entonces ambos operandos son evaluados
|
|
como <type>float</type>s y el resultado será un <type>float</type>. De otra manera,
|
|
los operandos seran interpretados como <type>integer</type>s, y el resultado será
|
|
entonces <type>integer</type>. Tenga en cuenta que esto <emphasis>no</emphasis>
|
|
implica que se cambien los tipos de los propios operandos; el único cambio es en
|
|
como se evalúan los operandos y en el tipo de expresión en sí mismo.
|
|
</para>
|
|
|
|
<informalexample>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$foo = "0"; // $foo es string (ASCII 48)
|
|
$foo += 2; // $foo es ahora un integer (2)
|
|
$foo = $foo + 1.3; // $foo es ahora un float (3.3)
|
|
$foo = 5 + "10 Cerditos pequeñitos"; // $foo es integer (15)
|
|
$foo = 5 + "10 Cerdos pequeños"; // $foo es integer (15)
|
|
?>
|
|
]]>
|
|
<!-- bad example, no real operator (must be used with variable, modifies it too)
|
|
$foo++; // $foo is the string "1" (ASCII 49)
|
|
|
|
TODO: explain ++/- - behaviour with strings
|
|
|
|
examples:
|
|
|
|
++'001' = '002'
|
|
++'abc' = 'abd'
|
|
++'xyz' = 'xza'
|
|
++'9.9' = '9.0'
|
|
++'-3' = '-4'
|
|
- -'9' = 8 (integer!)
|
|
- -'5.5' = '5.5'
|
|
- -'-9' = -10 (integer)
|
|
- -'09' = 8 (integer)
|
|
- -'abc' = 'abc'
|
|
|
|
-->
|
|
</programlisting>
|
|
</informalexample>
|
|
|
|
<simpara>
|
|
Si considera confusos los últimos dos ejemplos anteriores, consulte
|
|
<link linkend="language.types.string.conversion">Conversión de cadenas a
|
|
números</link>.
|
|
</simpara>
|
|
|
|
<simpara>
|
|
Para forzar a que una variable sea evaluada como un determinado tipo, consulte la sección
|
|
<link linkend="language.types.typecasting">Forzado de tipos</link>. Para cambiar el tipo
|
|
de variable, consulte la función <function>settype</function>.
|
|
</simpara>
|
|
|
|
<para>
|
|
Para probar cualquiera de los ejemplos de esta sección, utilice la función
|
|
<function>var_dump</function>.
|
|
</para>
|
|
|
|
<note>
|
|
<para>
|
|
El comportamiento de la conversión automática en <type>array</type> está actualmente
|
|
sin definir.
|
|
</para>
|
|
|
|
<para>
|
|
Tambien, debido a que PHP soporta el indexado de <type>string</type> mediante
|
|
compensaciones mediante la misma sintaxis empleada en el indexado de <type>array</type>s,
|
|
los siguientes ejemplos son válidos para todas las versiones de PHP:
|
|
</para>
|
|
|
|
<informalexample>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$a = 'car'; // $a es un string
|
|
$a[0] = 'b'; // $a sigue siendo un string
|
|
echo $a; // bar
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</informalexample>
|
|
|
|
<para>
|
|
Consulte la sección <link linkend="language.types.string.substr">Acceso a cadenas mediante
|
|
caracteres</link> para más información.
|
|
</para>
|
|
</note>
|
|
|
|
<sect2 xml:id="language.types.typecasting">
|
|
<title>Forzado de tipos</title>
|
|
|
|
<para>
|
|
El forzado de tipos en PHP funciona de la misma manera que en C:, donde el nombre del
|
|
tipo deseado se escribe entre paréntesis antes de la variable que se quiera forzar.
|
|
</para>
|
|
|
|
<informalexample>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$foo = 10; // $foo es un integer
|
|
$bar = (boolean) $foo; // $bar es un boolean
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</informalexample>
|
|
|
|
<para>
|
|
Los siguientes forzados de tipos están permitidos:
|
|
</para>
|
|
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>(int), (integer) - forzado a <type>integer</type></simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>(bool), (boolean) - forzado a <type>boolean</type></simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>(float), (double), (real) - forzado a <type>float</type></simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>(string) - forzado a <type>string</type></simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>(array) - forzado a <type>array</type></simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>(object) - forzado a <type>object</type></simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>(unset) - forzado a <type>NULL</type> (PHP 5)</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
|
|
<para>
|
|
El forzado (binary) y el soporte del prefijo b fueron añadidos en PHP 5.2.1
|
|
</para>
|
|
|
|
<para>
|
|
Fíjese que se permiten las tabulaciones y espacios dentro de los paréntesis,
|
|
por lo que los siguientes ejemplos son funcionalmente equivalentes:
|
|
</para>
|
|
|
|
<informalexample>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$foo = (int) $bar;
|
|
$foo = ( int ) $bar;
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
|
|
<para>
|
|
Forzado literal de <type>string</type>s y variables a <type>string</type>s
|
|
binarios:
|
|
</para>
|
|
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$binary = (binary) $string;
|
|
$binary = b"binary string";
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</informalexample>
|
|
|
|
<note>
|
|
<para>
|
|
El lugar de forzar una variable a <type>string</type>, tambien se puede
|
|
encerrar la variable entre comillas dobles.
|
|
</para>
|
|
|
|
<informalexample>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$foo = 10; // $foo es un integer
|
|
$str = "$foo"; // $str es un string
|
|
$fst = (string) $foo; // $fst es tambien un string
|
|
|
|
// Esto muestra que "son lo mismo"
|
|
if ($fst === $str) {
|
|
echo "son lo mismo";
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</informalexample>
|
|
</note>
|
|
|
|
<para>
|
|
Puede que no sea obvio que sucede exactamente cuando se fuerzan ciertos tipos.
|
|
Para más información, consulte estas secciones:
|
|
</para>
|
|
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="language.types.boolean.casting">Conversión a booleano</link>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="language.types.integer.casting">Conversión a entero</link>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="language.types.float.casting">Conversión a número de punto flotante</link>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="language.types.string.casting">Conversión a cadena</link>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="language.types.array.casting">Conversión a array</link>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="language.types.object.casting">Conversión a objecto</link>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="language.types.resource.casting">Conversión a
|
|
recurso</link>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="language.types.null.casting">Conversión a NULL</link>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="types.comparisons">Tablas de comparación de tipos</link>
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
|
|
</sect2>
|
|
</sect1>
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|