Which of these is a better php function?
Given below are two PHP functions which basically does the same thing, What i want to know is which one of these is the best to use?
$lang is an array which stores other values
$lang = array(
'default' => 'This is the default text',
'site' =&开发者_开发问答gt; array(
'name' => 'Website',
'url' => 'website.com',
),
'logo' => 'images/logo',
);
and please note that i will be using one of these function throughout the application for displaying text as the application i am building support multiple languages.
Function 1:
function lang($text='default') {
global $lang;
$text = explode(',',$text);
if(!empty($text[1])) {
$newtext = $lang[$text[0]][$text[1]];
}
else {
$newtext = $lang[$text[0]];
}
echo $newtext;
}
Usage : <?php lang('default'); ?> or <?php lang('site,name'); ?>
Function 2:
function lang($text='default', $text2='') {
global $lang;
if(!empty($text2)) {
$newtext = $lang[$text][$text2];
}
else {
$newtext = $lang[$text];
}
echo $newtext;
}
Usage : <?php lang('default'); ?> or <?php lang('site','name'); ?>
In my opinion, second function is more logical by the arguments. And explode function in first one is slower than operations with arrays in second.
The second, but I would rather split it into two methods.
Ex:
GetLanguage($site)
returns an associative array of strings
And
GetLanguageKeyValue($site, $key)
returns value as string
I prefer function 2: it is far more obvious that there are two parameters.
精彩评论