Jump to content


Photo

Place a function between an echo


  • Please log in to reply
8 replies to this topic

#1 bob_the _builder

bob_the _builder
  • Members
  • PipPipPip
  • Advanced Member
  • 207 posts

Posted 27 September 2006 - 07:28 AM

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

#2 steveclondon

steveclondon
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 27 September 2006 - 07:57 AM

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.


#3 lessthanthree

lessthanthree
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationUK

Posted 27 September 2006 - 08:06 AM

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.
call me a safe bet, i'm betting i'm not

#4 Arancaytar

Arancaytar
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 27 September 2006 - 08:15 AM

Wasn't the question whether it was possible to put a function call inside an echo command, not the other way around?

Your example

echo 'some text smileys()';


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

echo 'some text ' . smileys();

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

#5 steveclondon

steveclondon
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 27 September 2006 - 08:18 AM

ops yea  right, must start to take more notice of what people are asking.

#6 bob_the _builder

bob_the _builder
  • Members
  • PipPipPip
  • Advanced Member
  • 207 posts

Posted 27 September 2006 - 12:27 PM

Hi,

function call inside an echo command sums it up ..

Basically I have html stored in a function

function smileys() {
echo 'html here';
}


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.

echo '<td colspan="2" align="center" valign="middle">'.buttons().'</td>''


messes up the table

I guess a function would be better than a variable?


Thanks

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 September 2006 - 03:05 PM

Instead of echoing the string from the function, return the string.
<?php
function smileys()
{
$var = 'html here';
return($var);
}

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

Ken

#8 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 27 September 2006 - 03:45 PM

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.

#9 bob_the _builder

bob_the _builder
  • Members
  • PipPipPip
  • Advanced Member
  • 207 posts

Posted 27 September 2006 - 09:21 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users