<?xml version="1.0"?>
     <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
     <channel>
      <link>http://www.bitacora.davidesperalta.com/</link>
      <title>David Esperalta - Entrada "Otra forma de escribir PHP" de la bitácora</title>
      <generator>Gesbit</generator>
      <description>Bitácora personal, software, opinión</description>
      <atom:link href="http://www.bitacora.davidesperalta.com/rss/" rel="self"
       type="application/rss+xml" />
    
      <item>
       <link>http://www.bitacora.davidesperalta.com/otra-forma-de-escribir-php/</link>
       <guid>http://www.bitacora.davidesperalta.com/otra-forma-de-escribir-php/</guid>
       <pubDate>Sat, 26 Jul 2008 04:20:54 +0200</pubDate>
       <title><![CDATA[ Otra forma de escribir PHP ]]></title>
       <description><![CDATA[<p>Uno que viene de <a title="Entradas en esta bitácora con la etiqueta Delphi" href="http://www.bitacora.davidesperalta.com/tag/delphi">Object Pascal, o Delphi</a>, está acostumbrado a los <em>"begin"</em> y <em>"end"</em>, que abren y cierran bloques de código, dicho mal y pronto. Sin embargo, una de las cosas que <a title="Entrada en esta bitácora" href="http://www.bitacora.davidesperalta.com/la-magia-del-lenguaje-php/">me enamoraron de PHP</a>, derivada del lenguaje C, hasta donde yo llego, es la utilización de llaves para encerrar bloques de código. Personalmente, prefiero esta última sintaxis, por ser más rápida de escribir y parecerme más sencilla, sin detrimento de la legibilidad del código fuente.</p>
<p>En cualquier caso, lo que quería decir, es que con PHP es posible utilizar ambas sintaxis, es decir, es posible escribir esto:</p><div class="gbhighlighcode"><div class="sourcecode"><pre class="php"><span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$condition</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  <span style="color: #808080; font-style: italic;">// $condition is True</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>
<p>Y, lo mismo daría escribir esto otro también:</p><div class="gbhighlighcode"><div class="sourcecode"><pre class="php"><span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$condition</span><span style="color: #66cc66;">&#41;</span> :
  <span style="color: #808080; font-style: italic;">// $condition is True</span>
<span style="color: #b1b100;">endif</span>;</pre></div></div>
<p>Al escribir de este modo, en detrimento de mis tan queridas llaves, podemos estar buscando cierta visibilidad que podría perderse si usamos llaves. Esto es sobre todo cierto si, por la razón que sea, mezclamos los lenguajes PHP y HTML. En este caso, código fuente como este:</p><div class="gbhighlighcode"><div class="sourcecode"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$condition</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  <span style="color: #808080; font-style: italic;">// HTML code here</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>
<p>Puede que resulte más confuso que esta otra "versión":</p><div class="gbhighlighcode"><div class="sourcecode"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$condition</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #000000; font-weight: bold;">?&gt;</span>
  <span style="color: #808080; font-style: italic;">// HTML code here</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>
<p>Sobre todo en el caso de que utilicemos varias condiciones, y, por tanto, sean menester cierta cantidad de llaves. Cabe añadir que no sólo está disponible esta forma de escribir para las condiciones "if", sino que también existen los "endfor", "endwhile" o "endforeach". Sería como una especie de "comentario" que en realidad no es tal. Es común escribir cosas como:</p><div class="gbhighlighcode"><div class="sourcecode"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$condition</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  <span style="color: #808080; font-style: italic;">// HTML code here</span>
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$values</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$value</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    <span style="color: #808080; font-style: italic;">// HTML code here  </span>
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #808080; font-style: italic;">/* end for */</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #808080; font-style: italic;">/* end if */</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>
<p>Sobre todo si se utilizan varias condiciones y bucles. Pues bien, ya digo, sobre todo cuando existe código HTML de por medio, y cerramos las etiquetas que dan comienzo y finalizan el código PHP, creo que se gana mucho en legibilidad si, directamente, utilizamos la sintaxis opcional que nos ofrece el estupendo lenguaje PHP:</p><div class="gbhighlighcode"><div class="sourcecode"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$condition</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #000000; font-weight: bold;">?&gt;</span>
  <span style="color: #808080; font-style: italic;">// HTML code here</span>
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$values</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$value</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #000000; font-weight: bold;">?&gt;</span>
    <span style="color: #808080; font-style: italic;">// HTML code here  </span>
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>
<p>En definitiva, no creo que haya enseñado nada nuevo a nadie, aunque, todavía me queda la esperanza de que alguien que no supiera de esta posibilidad que ofrece PHP, pueda llevarse la grata sorpresa (creo que lo sería) de saber de conocerla, para usarla si se presenta la ocasión.</p>]]></description>
      </item>
      
     </channel>
    </rss>