Jump to content

Archived

This topic is now archived and is closed to further replies.

bob_the _builder

Place a function between an echo

Recommended Posts

Hi,

Is it possible to place a function holding html in between echo:

function smileys () {}

echo 'some stuff here smileys()';

I have tried a few ideas but no go, or should it just be held as a variable and place that in the echo instead?



Thanks

Share this post


Link to post
Share on other sites
you could do this

function smileys()
{
$ex='<br>some html<br>;
return $ex;
}

echo smileys;

or

function smileys()
{
echo '<br>some html<br>;
}

If you use the second method it can cause problems when out putting to tables etc. Sometimes it does some strange stuff.

Share this post


Link to post
Share on other sites
As well as causing problems with tables like you mentioned, it's generally not seen as a good idea to ever echo/print inside a function unless it's for debugging purposes. Return the value, then print that.

Share this post


Link to post
Share on other sites
Wasn't the question whether it was possible to put a [i]function call[/i] inside an [i]echo command[/i], not the other way around?

Your example

[code]echo 'some text smileys()';[/code]


would work with just one small change. You can't put a function call inside quotation marks. So you'd do this:

[code]echo 'some text ' . smileys();[/code]

In case you've never used the "." operator before: When you put it between string values (in this case 'some text' and the return value of smileys), it concatenates them into a single string. "hello" . "world" is "helloworld".

Share this post


Link to post
Share on other sites
Hi,

function call inside an echo command sums it up ..

Basically I have html stored in a function

[code=php:0]function smileys() {
echo 'html here';
}[/code]


when I call it into my table it messes up the structure, if I store the same code in a variable and call the variable into the table it places it perfect.

[code=php:0]echo '<td colspan="2" align="center" valign="middle">'.buttons().'</td>''[/code]


messes up the table

I guess a function would be better than a variable?


Thanks

Share this post


Link to post
Share on other sites
Instead of echoing the string from the function, return the string.
[code]<?php
function smileys()
{
$var = 'html here';
return($var);
}

echo '<td colspan="2" align="center" valign="middle">'.smileys().'</td>';
?>[/code]

Ken

Share this post


Link to post
Share on other sites
ken's right (obviously).

in short, the reason is the classic order of operations issue.  for example, 1 + 1 * 4 is 5, not 8, because the multiplication must take place first.

likewise here.  when using a command like echo, the string to be echoed is determined before the echo process is actually executed.  this is what makes variable interpolation (ie. echo "stuff is $var") possible, as far as i know.  therefore it will go through and execute any variable interpolation, functions, etc. FIRST because it's trying to settle on the string it needs to echo.  in other words, your current code will do this:

echo 'stuff '. smileys() . ' more stuff'

1.  execute smileys() to determine its returned values
2.  start the string with 'stuff '
3.  append the returned value from smileys() [none here]
4.  append ' more stuff' to the string
5.  echo the final string

so you see the fact that it runs smileys() before assembling the string and echoing it leads to 'html here' being echoed outside of the overall string.

i really hope you don't just ignore this and take the solution, because this is a fairly basic feature of php and programming in general that you should know.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.