1
$lang['profil_basic_medeni'] = array( 
    1 => 'Bekâr',
    2 => 'Evli',
    3 => 'Nişanlı',
    4 => 'İlişkide',
    5 => 'Ayrılmış',
    6 => 'Boşanmış'
    );
 $lang['profil_basic_sac'] = array( 
    1 => 'Normal',
    2 => 'Kısa',
    3 => 'Orta',
    4 => 'Uzun',
    5 => 'Fönlü',
    6 => 'Saçsız (Dazlak)',
    7 => 'Karışık/Dağınık',
    8 => 'Her Zaman Bol Jöleli :)'
    );

function sGetVAL($item,$valno) {
  $sonuc = $lang[$item][$valno];
  return $sonuc;
} 

$tempVAL1 = sGetVAL('profil_basic_medeni','3'); // return null
//or
$tempVAL2 = sGetVAL('profil_basic_sac','7'); // return null

$tempVAL1 or $tempVAL2 always return null. why ? how to fix function sGetVAL ???

1
  • Why do you need the function anyway? You could directly access the values. Commented Jan 2, 2011 at 18:56

4 Answers 4

3

Because you're using literal indexes like numeric indexes? Because the array $lang is not visible in function?

try this:

$tempVAL1 = sGetVAL('profil_basic_medeni',3); // return null
//or
$tempVAL2 = sGetVAL('profil_basic_sac',7); // return null

and this:

function sGetVAL($item,$valno) {
   global $lang;
   $sonuc = $lang[$item][$valno];
   return $sonuc;
} 
Sign up to request clarification or add additional context in comments.

7 Comments

You can interchange numeric string indexes with numerical ones. There is no difference. $a['1'] == $a[1]
He could also pass the array in to the function ... globals are evil :p
yup, both comments are right. But this is the way of smallest pain for him, probably :)
@Erik: Why did you delete your answer?
3 other similar answers posted first, I didn't think my version taking the array as a param was a significant enough of an improvment to leave up.
|
2

your array is global, but your function uses a local version of it (which is different and uninitialized).

either write global $lang first in your function, or use $GLOBALS['lang'] to access the array.

Comments

1

$lang is a global variable, that is not visible to sGetVal. Functions can usually only see variables that they define themselves (and Superglobals like $_POST and $_GET).

You could use

function sGetVAL($item,$valno) {
  global $lang;
  $sonuc = $lang[$item][$valno];
  return $sonuc;
}

but it would be better to do without global variables altogether.

Comments

0

The sGetVal function can't see the array $lang as you haven't used the global keyword to bring it into scope. Read here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.