bob_the _builder Posted September 27, 2006 Share Posted September 27, 2006 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 Quote Link to comment Share on other sites More sharing options...
steveclondon Posted September 27, 2006 Share Posted September 27, 2006 you could do thisfunction 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. Quote Link to comment Share on other sites More sharing options...
lessthanthree Posted September 27, 2006 Share Posted September 27, 2006 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. Quote Link to comment Share on other sites More sharing options...
Arancaytar Posted September 27, 2006 Share Posted September 27, 2006 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". Quote Link to comment Share on other sites More sharing options...
steveclondon Posted September 27, 2006 Share Posted September 27, 2006 ops yea right, must start to take more notice of what people are asking. Quote Link to comment Share on other sites More sharing options...
bob_the _builder Posted September 27, 2006 Author Share Posted September 27, 2006 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 tableI guess a function would be better than a variable?Thanks Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 27, 2006 Share Posted September 27, 2006 Instead of echoing the string from the function, return the string.[code]<?phpfunction smileys(){$var = 'html here';return($var);}echo '<td colspan="2" align="center" valign="middle">'.smileys().'</td>';?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
akitchin Posted September 27, 2006 Share Posted September 27, 2006 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 values2. start the string with 'stuff '3. append the returned value from smileys() [none here]4. append ' more stuff' to the string5. echo the final stringso 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. Quote Link to comment Share on other sites More sharing options...
bob_the _builder Posted September 27, 2006 Author Share Posted September 27, 2006 Hi,Ok I got it, prior I just had it as a variable, so just place the variable into the function rather than echo the html out within the function.Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.