0

I have two simple PHP functions:

<? 
  function print_txt($text) {
    echo $text;
  }

  function store_data($name,$data){
    define($name,$data);
  }
?>

I want the second function to store data and use it later whenever I want but when I try to put the first function as a parameter it prints it directly.

E.g : store_data('my_data',print_text('hello world')); when I reload the page the 'hello world' string appears

Is there any way to keep the data and control it ? Something like output buffering maybe ?

Renaud
  • 16,073
  • 6
  • 81
  • 79
Joseph
  • 20
  • 3

1 Answers1

0

The method echo do not have any return value. Return the value in print_txt after you have print it:

return $text;

Or did not I get your question?

Richard
  • 2,840
  • 3
  • 25
  • 37
  • but if i want to use only the first function to print strings / numbers it won't work ? i hope you get exactly what i want – Joseph May 04 '15 at 14:16
  • This would work either. If you are not in an output buffering block, _print_txt($txt)_ echoes _$txt_ and returns it (the return is irrelevant). For you example `store_data('my_data',print_text('hello world'));`, 'hello world' would be echoed and saved in the constant 'my_data'. – Richard May 04 '15 at 17:59