user3796133
user3796133

Reputation: 348

What is the use of a variable function in PHP?

I understand how to implement a variable function though i don't understand it's use. Why call a function using a variable than to call the function itself?

Unless to dynamically call functions from user input or returned database results?

Upvotes: 0

Views: 47

Answers (1)

Alaa Morad
Alaa Morad

Reputation: 455

EXAMPLE : if you have an input like /?do=something

require_once('do.php');
$fun = 'do_'.$_GET['do'];
if (function_exists($fun)) {
    $fun(); //variable function
} else {
    not_found();
}

so in this case I just add a function to my do.php file and it will be ready to use

do.php :

<?php
function do_getkey() {
    // do something when do=getkey
}
function do_sendkey() {
    // do something when do=sendkey
}
 function not_found() {
    // when not found 
 }
?>

Upvotes: 1

Related Questions