如何在echo调用中运行函数

时间:2010-08-04 14:21:27

标签: php

这是一个快速而简单的问题。我有一个糟糕的一天,不知道该怎么做:

我需要做的是......

我正在检查网址中的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()函数?

谢谢,

马特

5 个答案:

答案 0 :(得分: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; ?>

答案 1 :(得分: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 {

答案 2 :(得分:1)

您的问题是您在另一个PHP标记内打开PHP标记(<?php)(您需要先使用?>关闭。

一种方法是做这样的事情(为了便于阅读,我简化了你的例子):

<?php

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

?>

需要考虑的一些要点:

  • 通常在向url添加参数时使用urlencode但mt_rand返回整数,因此在这种情况下我们不需要。
  • 在回声中,通常最好将其与,一起使用,而不是使用.然后echo将结果字符串连接起来。这样,使用的内存更少,操作也更少(但这超出了本问题的范围)。

答案 3 :(得分: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 { ?>

不要忘记摆脱所有这些丑陋的斜线

答案 4 :(得分:1)

你不应该用echo添加大块静态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 {//...
        }
?>