开发者

passing data into a function

开发者 https://www.devze.com 2023-03-06 20:19 出处:网络
Okay So I know this is super basic and I should know how to do this but I\'m blanking and having trouble finding the answer in Google. I have an include that has an array of variables for example

Okay So I know this is super basic and I should know how to do this but I'm blanking and having trouble finding the answer in Google. I have an include that has an array of variables for example

 $phrases["text"][1] = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?";
 $phrases["mp3"][1] = "http://example.com/file.mp3";

Th开发者_StackOverflow社区en a function that gets the varibles:

function return_phrase($phrase_name="", $fallback="",$default ="text"){
    $next= (isset($default) && $default =="mp3") ? 'text' : 'mp3';  
    if(isset($tts_phrases[$default][$phrase_name])){
        return $phrases[$default][$phrase_name]);
    }
    else if(isset($tts_phrases[$next][$phrase_name])){
        return $phrases[$next][$phrase_name]);
    }

    else{
        return $fallback;
    }
}

The problem is that the $phrases arrays aren't being sent to the function I can include the file in the function itself but I know that's the wrong way to do it. I think I need to use $global just not sure how.


Method 1: Pass $phrases, $tts_phrases as parameters

function return_phrase(array $phrases, array $ttphrases, $phrase_name="", $fallback="",$default ="text"){
    $next= (isset($default) && $default =="mp3") ? 'text' : 'mp3';  
    if(isset($tts_phrases[$default][$phrase_name])){
        return $phrases[$default][$phrase_name]);
    }
    else if(isset($tts_phrases[$next][$phrase_name])){
        return $phrases[$next][$phrase_name]);
    }

    else{
        return $fallback;
    }
}

Method 2: Make $phrases, $tts_phrases global (bad!)

function return_phrase($phrase_name="", $fallback="",$default ="text"){
    global $phrases, $tts_phrases;
    $next= (isset($default) && $default =="mp3") ? 'text' : 'mp3';  
    if(isset($tts_phrases[$default][$phrase_name])){
        return $phrases[$default][$phrase_name]);
    }
    else if(isset($tts_phrases[$next][$phrase_name])){
        return $phrases[$next][$phrase_name]);
    }

    else{
        return $fallback;
    }
}

Using global variables is a quick and easy fix, but once your application gets larger they become very hard to keep track of. For example, take this legacy code snippet that I have to deal with at work:

function foo() {
     global $mysqldsn, $ldapdsn, $autologout_timer, $isMobileDevice, logout_fail_limit, $logout_fail_window, $lang, $project_contact_email, $project_contact_name ... (50 or 60 more global variables following)
     ...
 }

Any time I am looking at one of the pages that just pulls one of those variables out of thin air, I have to Ctrl+F the whole project and make sure that every little change hasn't messed up the whole app. When you keep your variables in local scope, you know exactly what you are changing.

0

精彩评论

暂无评论...
验证码 换一张
取 消