Autosetting Function Names based on Parameter Input in PHP

I am making an AJAX script right now. I have an html file, a javascript file, and a PHP file.The JavaScript file will have a function will pass the parameter request_name to a URL. The PHP script that catches the request has a switch statement that calls a different function based on request_name value. The functions names are the …

via PHP Website Development » Search Results » ajax:

Autosetting Function Names based on Parameter Input in PHP

I am making an AJAX script right now. I have an html file, a javascript file, and a PHP file.
The JavaScript file will have a function will pass the parameter request_name to a URL. The PHP script that catches the request has a switch statement that calls a different function based on request_name value. The functions names are the request_name values, i.e. if $_REQUEST[‘request_name’] is “test_function”, then the switch statement calls test_function().
Since the parameter name value will be the same as the function name, is there any way to basically just have PHP call request_name_value if that parameter is set. It seems easier than having a switch statement, espeically since there will be a lot of functions.
…………………………………..

Try this:
$func = “mynamespace_”.$_REQUEST[“request_name”];
if (function_exists($func))
$func();
else
header(“Location: /index.html”);
exit;

For more info: Autosetting Function Names based on Parameter Input in PHP

PHP Website Development » Search Results » ajax

Autosetting Function Names based on Parameter Input in PHP

Share this post:

Related Posts

Leave a Comment