1
0
mirror of https://github.com/php/doc-es.git synced 2026-04-30 02:23:10 +02:00
Files
2025-05-06 22:42:56 +02:00

105 lines
3.5 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: 22583751fbfdaa3eaa41aeb6470d1343f5cb2c78 Maintainer: PhilDaiguille Status: ready -->
<!-- Reviewed: yes -->
<sect1 xml:id="control-structures.alternative-syntax" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Sintaxis alternativa</title>
<?phpdoc print-version-for="control-structures.alternative-syntax"?>
<para>
PHP ofrece otra manera de agrupar instrucciones dentro de un bloque, para las funciones de control <literal>if</literal>,
<literal>while</literal>, <literal>for</literal>, <literal>foreach</literal> y <literal>switch</literal>.
En cada caso, el principio es reemplazar la llave de apertura por dos puntos (:) y la llave de cierre por, respectivamente,
<literal>endif;</literal>, <literal>endwhile;</literal>, <literal>endfor;</literal>, <literal>endforeach;</literal>, o
<literal>endswitch;</literal>.
<informalexample>
<programlisting role="php">
<![CDATA[
<?php if ($a == 5): ?>
A igual 5
<?php endif; ?>
]]>
</programlisting>
</informalexample>
</para>
<simpara>
En el ejemplo anterior, el bloque HTML "A igual 5" se incluye dentro de un <literal>if</literal> utilizando esta nueva sintaxis. Este código HTML solo se mostrará si la variable <varname>$a</varname> es igual a 5.
</simpara>
<para>
Esta otra sintaxis también funciona con <literal>else</literal> y <literal>elseif</literal>. El siguiente ejemplo muestra una estructura con un <literal>if</literal>, un <literal>elseif</literal> y un <literal>else</literal> utilizando esta otra sintaxis:
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
if ($a == 5):
echo "a igual 5";
echo "...";
elseif ($a == 6):
echo "a igual 6";
echo "!!!";
else:
echo "a no vale ni 5 ni 6";
endif;
?>
]]>
</programlisting>
</informalexample>
</para>
<note>
<para>
No se puede utilizar diferentes sintaxis en el mismo bloque de control.
</para>
</note>
<warning>
<para>
Cualquier visualización (incluyendo espacios) entre una estructura <literal>switch</literal> y el primer <literal>case</literal> producirá un error de sintaxis. Por ejemplo, esto no es válido:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php switch ($foo): ?>
<?php case 1: ?>
// ...
<?php endswitch; ?>
]]>
</programlisting>
</informalexample>
<para>
Mientras que esto es válido, ya que la última nueva línea después de la estructura <literal>switch</literal> se considera parte de la etiqueta de cierre <literal>?&gt;</literal> y, por lo tanto, no se muestra nada entre <literal>switch</literal> y <literal>case</literal>:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php switch ($foo): ?>
<?php case 1: ?>
...
<?php endswitch; ?>
]]>
</programlisting>
</informalexample>
</warning>
<para>
Ver también <link linkend="control-structures.while">while</link>, <link linkend="control-structures.for">for</link>, y <link linkend="control-structures.if">if</link> para otros ejemplos.
</para>
</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
-->