Jump to content

Place a function between an echo


bob_the _builder

Recommended Posts

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".
Link to comment
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
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.