It so happened that I need to output in the middle of the page the result of the function depending on the value of a variable that takes its value from $ _GET, the function does different things.

For example:

PHP код, ла ла ла switch ($get) { case 1: function GET() { ла ла ла } break; case 2: function GET() { ту ту ту } break; ... и т.д. } 

And it all comes out like this:

 <html> <body> <? GET(); ?> </body> </html> 

Moreover, in the function there is sometimes a sending of headers (header), and as you know, sending headers after there has already been output to the browser is not recommended. Well, the question is - how do you do this right? When PHP is installed as an Apache module - headers are sent with joy, and when, like CGI, output buffering saves.

PS By the way, does anyone know what to fear when using the ob_start() function? Can it create any serious load? Or other problems? Buffer overflow?

  • one
    And it’s all displayed like this: <html> <body> {function: some_function (some, function, variables)} </ body> </ html> That's when it’s displayed like this, then steeper! I will support the answer to @razielsd and say that writing a template engine is not so difficult! It is then nice to separate logic from presentation! - Palmervan

3 answers 3

 function get1() {} function get2() {} function get3() {} function get4() {} // допустим $_GET['n'] всегда число $method = 'get' . $_GET['n']; if ( function_exists($method) ) { $method(); } 
  • The @exec function is always called the same. Just depending on the value of the variable, the bowel functions change. :) But thanks anyway, I learned something new. :) - Angelina_Jo
  • Pass the required parameter to the function argument, create the necessary conditions in the function. - Oleg
  • @exec I probably didn’t manage to ask the right question. First comes the logic, the definition of a user-defined function, what exactly a function does depends on the variable, but it does not matter. The main thing is that the function is called in the script already when it was output to the browser, and after all, the function itself uses headers. It turns out that the headers are trying to go after the withdrawal. I’m wondering how to redesign the logic so that at first it was decided that yes how, all conditions, all redirections, and then the result was implemented in the middle of html, which had already begun to be displayed. - Angelina_Jo
  • Use the template engine. - Oleg
 $getMethods = array( '1' => function(){}, '2' => function(){}, '3' => function(){} /* N => function(){} */ ); $requestGetMethod = $_GET['n']; if(!empty( $getMethods[ $requestGetMethod ] )) { $getMethods[ $requestGetMethod ] (); // .......... } 

Dear - are you kidding or what? The fact that you do not know that php has nameless functions and the fact that such a design is possible and works fine is not my problem. You would start first, and then you would make your "ingenious" conclusions.

Especially for those who are not in the subject, (works in php 5.3+)

  $hello = function(){ echo 'HelloWorld'; } $hello(); // -> HelloWorld 

As I understand, both of you should pay a little attention to the issue of nameless functions in PHP, I hope now there will be no more such silly questions and resentments

  • @FLK, which is a great pity. - Oleg
  • @AlexWindHope apologize on the wrong server launched (there 5.2). therefore, mistakenly concluded that anonymous functions, therefore, cannot be stored in an array. - FLK
  • one
    Yes, I have nothing against it, but one thing is not to know and, for example, to write - it does not work for me, etc., it’s quite another pathos to declare that it is impossible =) - Zowie
  • Oh ......! - Oleg
  • @AlexWindHope 90% of my projects work on 5.2.17 so the "unacceptable" is the very word. although in the coming week they promise 5.4 to put on the hosting where there are 24 projects. will be more fun) - FLK

ob_ * functions work fine, and there are no problems with them, many templates work on their basis, use it.

  • @razielsd, thanks. :) And for sure? I’d just like to imagine that php first saves the output somewhere, and then only prints it seems that it’s hard for him and it’s better not to use it once again. Does this buffer have a limit? How to find out? He may not be enough? - Angelina_Jo
  • one
    There is no significant difference, in fact, use as you like - Zowie