将数据传递给函数

发布于 2024-11-07 23:56:56 字数 807 浏览 0 评论 0原文

好的,所以我知道这是非常基本的,我应该知道如何做到这一点,但我一片空白,无法在谷歌中找到答案。例如,我有一个包含变量数组的包含文件

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

,然后是一个获取变量的函数:

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;
    }
}

问题是 $phrases 数组没有发送到该函数,我可以将该文件包含在函数本身,但我知道这是错误的方法。我想我需要使用 $global 只是不知道如何使用。

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";

Then 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

策马西风 2024-11-14 23:56:56

方法 1:将 $phrases、$tts_phrases 作为参数传递

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;
    }
}

方法 2:使 $phrases、$tts_phrases 全局化(不好!)

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;
    }
}

使用全局变量是一个快速且简单的解决方案,但是一旦您的应用程序变得更大,它们就会变得很难跟踪。例如,以我在工作中必须处理的这个遗留代码片段为例:

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)
     ...
 }

每当我查看某个页面时,它只是凭空提取其中一个变量,我必须对整个项目按 Ctrl+F 并制作确保每一个小变化都不会弄乱整个应用程序。当您将变量保留在局部范围内时,您就可以确切地知道要更改的内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文