Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a function within an echo call

Tags:

php

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

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

What do I need to do to get that mt_rand() function to fire?

Thanks,

Matt

like image 903
TikaL13 Avatar asked Aug 04 '10 14:08

TikaL13


People also ask

How do you echo a function?

Definition and Usage. The echo() function outputs one or more strings. Note: The echo() function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo(), using parentheses will generate a parse error.

How do you call a function within a function in PHP?

If you define function within another function, it can be accessed directly, but after calling parent function. For example: function a () { function b() { echo "I am b."; } echo "I am a.

Can you use echo as a function and pass multiple parameters to it?

echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses.

Can I have a function within a function in PHP?

You can also define a function within another function and logically this too should be a local variable but recall the rule that all functions are global. In PHP inner functions are global and hence behave in the same way as if they had been declared outside of any containing function.


2 Answers

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 &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; ?>
like image 98
Dan Breen Avatar answered Sep 30 '22 12:09

Dan Breen


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 &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 {
like image 37
Pavel Morshenyuk Avatar answered Sep 30 '22 11:09

Pavel Morshenyuk