2010-08-04 31 views
5

Esta es una pregunta rápida pero sencilla. Estoy teniendo un mal día y no sé cómo hacer esto:Cómo ejecutar una función dentro de una llamada de eco

Lo que necesito hacer es esto ...

estoy comprobando para PMD en la url de ser así eco de esto:

<?php 
     if (isset($_GET['pmd'])) { 
      echo"<div class=\"response\"><p style=\"width:350px; height:200px; vertical-align:middle;\"><strong>Thank you for acting as a &quot;watchman on the walls&quot; of Jerusalem! Your name will now appear on our virtual wall.<br><br>You can also catch up on the latest news from Israel by visiting our news feed below.</strong></p></div>"; 
     }else { etc... 

Lo que necesito incluir dentro de eso es una imagen como tal que está disparando un píxel de seguimiento.

<img src="url.com?cid=12345&aid=1234&oid=<?php echo mt_rand(); ?>&quantity=1" height="1" width="1" /> 

Lo que me gustaría tener es:

<?php 
     if (isset($_GET['pmd'])) { 
      echo"<div class=\"response\"><p style=\"width:350px; height:200px; vertical-align:middle;\"><strong>Thank you for acting as a &quot;watchman on the walls&quot; of Jerusalem! Your name will now appear on our virtual wall.<br><br>You can also catch up on the latest news from Israel by visiting our news feed below.</strong></p> 
      <br /> 
      <img src="url.com?cid=12345&aid=1234&oid=<?php echo mt_rand(); ?>&quantity=1" height="1" width="1" /> 
      </div>"; 
     }else { 

¿Qué tengo que hacer para conseguir que la función() mt_rand al fuego?

Gracias,

Matt

+0

Debe utilizar '&' en las URL, no '&'. – Artefacto

+0

Si no quiere escapar de las comillas de la cadena (lo hace más difícil de leer), simplemente encierre la cadena en ''' en vez de' "'. –

Respuesta

11

Usted podría utilizar esta sintaxis en su lugar:

<?php if (isset($_GET['pmd'])): ?> 
    <div class="response"><p style="width:350px; height:200px; vertical-align:middle;"><strong>Thank you for acting as a &quot;watchman on the walls&quot; of Jerusalem! Your name will now appear on our virtual wall.<br><br>You can also catch up on the latest news from Israel by visiting our news feed below.</strong></p> 
     <br /> 
     <img src="url.com?cid=12345&aid=1234&oid=<?php echo mt_rand(); ?>&quantity=1" height="1" width="1" /> 
    </div> 
<?php else: ?> 
    etc 
<?php endif; ?> 
+0

Gracias por su respuesta – TikaL13

3

Algo como esto:

<?php 
      if (isset($_GET['pmd'])) { 
       echo "<div class=\"response\"><p style=\"width:350px; height:200px; vertical-align:middle;\"><strong>Thank you for acting as a &quot;watchman on the walls&quot; of Jerusalem! Your name will now appear on our virtual wall.<br><br>You can also catch up on the latest news from Israel by visiting our news feed below.</strong></p> 
       <br /> 
       <img src=\"url.com?cid=12345&aid=1234&oid=".mt_rand()."&quantity=1\" height=\"1\" width=\"1\" /> 
       </div>"; 
      }else { 
+0

agregue un espacio después del eco – Emyr

1

Su problema es que está abriendo una etiqueta PHP (<?php) dentro de otra etiqueta PHP (que necesitan para cerrar con ?> primero.

Una forma de hacerlo es hacer algo como esto (simplifiqué su ejemplo de legibilidad):

<?php 

echo '<img src="foo.jpg?oid=', mt_rand(), '">'; 

?> 

Algunos puntos a considerar:

  • lo general, se utilizan urlencode cuando se agrega parámetros a una URL, pero mt_rand devuelve enteros por lo que no es necesario en este caso.
  • En un eco generalmente es mejor usarlo con , en lugar de congestionar con . y luego echo la cadena resultante. De esta forma, se utiliza menos memoria y se realizan menos operaciones (pero eso está fuera del alcance de esta pregunta).
1

de la misma manera que lo hizo - abriendo y cerrando etiquetas PHP

<?php if (isset($_GET['pmd'])) { ?> 
<div class=\"response\"><p style=\"width:350px; height:200px; vertical-align:middle;\"> 
<strong>Thank you for acting as a &quot;watchman on the walls&quot; of Jerusalem! 
Your name will now appear on our virtual wall.<br><br> 
You can also catch up on the latest news from Israel by visiting our news feed below.</strong></p> 
<br /> 
<img src="url.com?cid=12345&aid=1234&oid=<?php echo mt_rand(); ?>&quantity=1" height="1" width="1" /> 
      </div> 
<?php  }else { ?> 

no se olvide de deshacerse de todos estos feas barras diagonales

1

No debe agregar grandes bloques de HTML estático con eco. Pruebe:

<?php 
     if (isset($_GET['pmd'])) { 
?> 
      <div class="response"><p style="width:350px; height:200px; vertical-align:middle;"> 
      <strong>Thank you for acting as a &quot;watchman on the walls&quot; of Jerusalem! Your name will now appear on our virtual wall.<br><br>You can also catch up on the latest news from Israel by visiting our news feed below.</strong></p> 
      <br /> 
      <img src="url.com?cid=12345&aid=1234&oid=<?php echo mt_rand(); ?>&quantity=1" height="1" width="1" /> 
      </div> 
<?php 
     }else {//... 
     } 
?> 
Cuestiones relacionadas