2010-08-04 40 views
5

這是一個快速而簡單的問題。我有一個糟糕的一天,不知道如何做到這一點:如何在回聲呼叫中運行功能

我需要做的是...

我在url檢查PMD如果是這樣迴應的:

<?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... 

我需要包含的內容是像這樣的圖像觸發跟蹤像素。

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

我想擁有的是:

<?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 { 

什麼我需要做的就是這mt_rand()函數來火?

感謝,

馬特

+0

您應該使用''&的網址,而不是'&'。 – Artefacto 2010-08-04 14:25:49

+0

如果你不想逃避字符串中的引號(使其更難讀),只需在'''而不是'''中加入字符串。 – 2010-08-04 14:54:46

回答

11

你可以使用這個語法來代替:

<?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

謝謝你的回覆 – TikaL13 2010-08-04 14:48:35

3

事情是這樣的:

<?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

在回顯後添加一個空格。 – Emyr 2010-08-04 14:25:57

1

你的問題是,你是另一個PHP標籤中打開一個PHP標籤(<?php)(您需要關閉與?>第一

做到這一點的一種方法是做這樣的事情(我簡化你的例子的可讀性):

<?php 

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

?> 

幾點考慮:

  • 通常你用urlencode當你添加參數的URL,但mt_rand返回一個整數,所以我們並不需要在這種情況下。
  • 在回聲中,通常最好將它與,配合使用,而不是將常量與.連接,然後使用echo結果字符串。這樣,使用更少的內存和更少的操作(但這超出了這個問題的範圍)。
1

以同樣的方式,你做到了 - 通過打開和關閉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 { ?> 

不要忘記擺脫這一切醜陋的斜線

1

你不應該添加大塊的靜態HTML與回聲。嘗試:

<?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 {//... 
     } 
?>