Jump to content


Photo

call function with variable


  • Please log in to reply
4 replies to this topic

#1 phpianer

phpianer
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 24 July 2006 - 06:06 PM

HI,

I want to call a function with a var

like: 
www......page.php?f=echo

$a=$_GET['f'];
$a('asadsadasd');


is something like this possible in php??

Thanks

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 24 July 2006 - 06:36 PM

You can with eval:
<?php

if(isset($_GET['a']))
{
    $a = $_GET['a'];
    eval("{$a}('hey this works');");
}

?>
<br />
<a href="?a=echo">Test</a>
However be careful with the use of eval. Make sure there is no nasty code stored within the 'a' variable.

#3 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 24 July 2006 - 06:38 PM

<?
if(!empty($_GET['echo']) {
do_your_function();
} else {
do_something_else();
}
?>
[code][/code]


#4 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 24 July 2006 - 06:41 PM

if you've tried your code out, you should be able to answer yourself with a 'yes'... however, that's very dangerous strategy because i can change the url to some critical function name and screw up your server. so, it's better to use ChaosXero's method so that you have a layer of security over what function to be used.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#5 phpianer

phpianer
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 24 July 2006 - 07:09 PM

Yes thinks , i wil cotrole it with an array




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users