How to do it right?

It so happened that I need to display the result of the function in the middle of the page, depending on the value of the 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 looks like this:

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

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

P.S. By the way, does anyone know what to be afraid of when using the ob_start()function? Can she create any serious load? Or other problems? Buffer overflow?


Answer 1, authority 100%

function get1() {}
function get2() {}
function get3() {}
function get4() {}
//  $_GET['n']  
$method = 'get' . $_GET['n'];
if ( function_exists($method) ) {
  $method();
}

Answer 2, authority 100%

$getMethods = array(
   '1' => function(){},
   '2' => function(){},
   '3' => function(){}
   /* N => function(){} */
);
$requestGetMethod = $_GET['n'];
if(!empty( $getMethods[ $requestGetMethod ] )) {
    $getMethods[ $requestGetMethod ] ();
    // ..........
}

Dear, are you kidding me? The fact that you don’t know that there are unnamed functions in php and that such a construction is possible and works fine is not my problem. You would start first, and then you would already make your “brilliant” conclusions.

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

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

As I understand it, both of you should pay a little attention to the issue of anonymous functions in PHP, I hope now such stupid questions and indignation will no longer arise


Answer 3, authority 60%

ob_* functions work great, and there are no problems with them, many template engines work on their basis, use them.

Leave a reply

Please enter your comment!
Please enter your name here