How to create a single function with different criteria on WordPress?

I’m newbie in PHP, so be kind please! And I’m sorry if it’s too repeated, but my brain is burning. I want to learn how to change the variables using only one function of WordPress.

This is my variables…

Read More
function be_comu() {
$name = 'BE';
echo $name;
}

function cn_comu() {
$name = 'CN';
echo $name;
}

function mn_comu() {
$name = 'MN';
echo $name;
}

This is my function and be_comu() is a variable that I want to change.

function turismo() {
ob_start();
be_comu();
$rota = ob_get_clean();

echo $rota;
}

How to change the result to cn_comu or mn_comu without creating a new function?

Related posts

1 comment

  1. With call_user_func (PHP is awesome that way):

    function turismo($func_name) {
        // First, let's be sure the function actually exists to prevent fatal errors
        if ( ! function_exists($func_name) ) {
            echo "Whoah! Can't call a function that doesn't exist!";
            return;
        }
    
        // While output buffering is cool, doesn't have any value here...
        ob_start();
        // use call_user_func to call the function name we passed in...
        call_user_func($func_name);
        $rota = ob_get_clean();
    
        echo $rota;
    }
    
    // Results:
    turismo('be_comu');
    // outputs "BE"
    
    turismo('cn_comu');
    // outputs "NE"
    
    turismo('non_existent_function');
    // Outputs "Whoah! Can't call a function that doesn't exist!"
    

Comments are closed.