How to run a function within an echo call
This is a quick but simple question. I am having a bad day and don't know how to do this:
What I need to do is this...
I am checking for PMD in the url if so echo this:
<?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 "watchman on the walls" 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...
What I need to include within that is an image like such that is firing off a tracking pixel.
<img src="url.com?cid=12345&aid=1234&oid=<?php echo mt_rand(); ?>&quantity=1" height="1" width="1" />
What I would like to have is:
<?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 "watchman on the walls" of Jerusalem! Your name will now appear on our virtual wall.<br><br&g开发者_开发技巧t;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 {
What do I need to do to get that mt_rand() function to fire?
Thanks,
Matt
You could use this syntax instead:
<?php if ( isset($_GET['pmd']) ): ?>
<div class="response"><p style="width:350px; height:200px; vertical-align:middle;"><strong>Thank you for acting as a "watchman on the walls" 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; ?>
Something like this:
<?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 "watchman on the walls" 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 {
the same way you did it - by opening and closing PHP tags
<?php if ( isset($_GET['pmd']) ) { ?>
<div class=\"response\"><p style=\"width:350px; height:200px; vertical-align:middle;\">
<strong>Thank you for acting as a "watchman on the walls" 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 { ?>
don't forget to get rid of all these ugly slashes
You shouldnt add large blocks of static HTML with echo. Try:
<?php
if ( isset($_GET['pmd']) ) {
?>
<div class="response"><p style="width:350px; height:200px; vertical-align:middle;">
<strong>Thank you for acting as a "watchman on the walls" 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 {//...
}
?>
Your problem is that you are opening a PHP tag (<?php
) inside another PHP tag (you need to close with ?>
first.
One way to do it is to do something like this (I simplified your example for readability):
<?php
echo '<img src="foo.jpg?oid=', mt_rand(), '">';
?>
Some points to consider:
- Usually you use urlencode when you add parameters to an url but mt_rand returns integers so we don't need to in this case.
- In a echo it's generally better to use it with
,
instead of concatening consts with.
thenecho
the resulting string. This way, less memory is used and less operations are done (but that's out of the scope of this question).
精彩评论