Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a $_GET value as a function call 1

Status
Not open for further replies.

Bastien

Programmer
May 29, 2000
1,683
CA
Hi All,

I am wondering how I can take an input parameter passed into a script and use it as a function call? I know that I could use a switch statement, but my goal is to make this as fluid as possible...

Any ideas?

Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 
actually, you don't need eval. This is sufficient:

Code:
$_GET['fun']();

please remember always to enquote your array keys when making use of associative arrays.
 
Hi

jpadie said:
actually, you don't need eval.
Man, how stupid I am ! While I wrote my previous post I was thinking that "would be nice to just call it directly, without [tt]eval[/tt]...". And I did not tried it. [mad] Thank you, jpadie. [medal]
jpadie said:
please remember always to enquote your array keys when making use of associative arrays.
I know, I already read the related chapter in the PHP documentation. First... I think about 3 years ago.


Personally I will keep using the hash keys unquoted. The documentation's argumentation does not convince me. Anyway, since my early C days I use all uppercase constant names ( and of course lowercase or CamelCase variable names ), so there will be no problem with later [tt]define()[/tt] calls.

I enjoy this bad coding style and I think without this possibility the PHP language would be poorer.

Feherke.
 
the problem is that php will throw a notice if you do not enquote keys and there is no constant defined. although you can suppress this with an appropriate setting of error_reporting, you are still adding to processor overhead during the parsing process.
 
Hi

Thank you, I am aware of both.

I am not simply disagreeing with recommendation/good coding style/logic. I tried doing it the right way too ( last time a week ago, because I was temporarily lazy to set [tt]error_reporting[/tt]... ) but I end up turning back to my bad old style. I find it easier to develop/maintain.

Feherke.
 
no criticism intended. but since this is a public thread and all sorts from beginners upwards may read it, I wanted to explain why I considered that your route was not preferred.
 
what about the function call_user_func()?


Code:
if (function_exists($_GET['f'])){
call_user_func($_GET['f']);
}



Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top