1

Good afternoon guys! I’ve been trying to call a function and it just doesn’t seem to work.

So, this is the code I’ve got:

<? 
if (!isset($_SESSION)) session_start();
$acess_level = 4;
if (!isset($_SESSION['UserID']) OR ($_SESSION['UserAL'] < $acess_level)) {
    session_destroy();
    header("Location: login"); exit;
}


$tlang = $_SESSION['lang'];
$level = $_SESSION['UserAL'];
include($_SERVER["DOCUMENT_ROOT"] . "/inside/functions.php");
$check = "adv";

function doLang()
{
  if ($tlang == 'en') {echo "Advanced Tutorials";} 
  if ($tlang == 'br') {echo "Tutoriais Avançados";} 
  if ($tlang == 'es') {echo "Tutoriales Avanzados";} 
  if ($tlang == 'fr') {echo "Tutos avancés";} 
  if ($tlang == 'id') {echo "Pelatihan dengan cara proffesional";} 
  if ($tlang == 'fi') {echo "Edistyneempiä oppaita";} 
  if ($tlang == 'tr') {echo "Gelişmiş Kılavuzlar";}
}

doLang();
echo "test".doLang()."test"; ?>

The "session" part is working correctly so I guess there ain’t no typo/problem there at all. So, basically, the doLang() function checks what language the user is using and then echos the ‘‘right’’ language, as you can see. If I use something like this:

<?
  if ($tlang == 'en') {echo "Advanced Tutorials";} 
  if ($tlang == 'br') {echo "Tutoriais Avançados";} 
  if ($tlang == 'es') {echo "Tutoriales Avanzados";} 
  if ($tlang == 'fr') {echo "Tutos avancés";} 
  if ($tlang == 'id') {echo "Pelatihan dengan cara proffesional";} 
  if ($tlang == 'fi') {echo "Edistyneempiä oppaita";} 
  if ($tlang == 'tr') {echo "Gelişmiş Kılavuzlar";}
?>

It works like a charm but if I use a function instead it just doesn’t work. Am I missing something, did I do something wrong? Thanks for your attention!

5
  • 4
    You need to pass $tLang as an argument to the function - read about variable scope Commented Aug 29, 2013 at 15:32
  • also, consider doing a switch() statement. Commented Aug 29, 2013 at 15:35
  • 1
    And return the value instead of echoing it. Commented Aug 29, 2013 at 15:41
  • @ChristianGärtner Sorry but could you teach me how to return it? @Robbert’s return function is not working :\ Commented Aug 29, 2013 at 16:13
  • @MisaelVergara, I fixed the function. I left out some quotes. Commented Aug 29, 2013 at 16:37

2 Answers 2

6

$tLang is not in the scope of the function. You need to either pass the variable to the function

function doLang($tLang) {
   ...
}

or use the global statement

function doLang() {
   global $tLang

   ....
}

The first method is the preferred method.

You really should consider changing the way you use this function. To clean it up, I would use a switch statement to make the flow a little nicer and I would return the value instead of echoing the result. Right now, since you're calling the function during an echo statement (echo "test".doLang()."test"), your function works, but if you were to ever use it in a variable assignment ($var = "text".doLang()."text"), your function would not work

function doLang($tLang)
{
   $val = "";
   switch($tLang) {
     case "br":
       $val = "Tutoriais Avançados"
       break;
     case "es":
       $val = "Tutoriales Avanzados";
       break;
     case "fr":
       $val = "Tutos avancés";
       break;
     case "id":
       $val = "Pelatihan dengan cara proffesional";
       break;
     case "fi":
       $val = "Edistyneempiä oppaita";
       break;
     case "tr":
       $val = "Gelişmiş Kılavuzlar";
       break;
     default:
       $val = "Advanced Tutorials";
       break;
   }
   return $val;
}

Using the default statement ensures that $val always has a value to return.

Sign up to request clarification or add additional context in comments.

Comments

1

Use this :

function doLang($tlang)
{
  if ($tlang == 'en') { echo "Advanced Tutorials"; } 
  if ($tlang == 'br') { echo "Tutoriais Avançados"; } 
  if ($tlang == 'es') { echo "Tutoriales Avanzados"; } 
  if ($tlang == 'fr') { echo "Tutos avancés"; } 
  if ($tlang == 'id') { echo "Pelatihan dengan cara proffesional"; } 
  if ($tlang == 'fi') { echo "Edistyneempiä oppaita"; } 
  if ($tlang == 'tr') { echo "Gelişmiş Kılavuzlar"; }
}

doLang($tlang);
?>

4 Comments

Note that if you're echoing the result of the function, you should probably return from rather than echo in the function
@MarkBaker : Yeah i had copied entire from the original post.. removed..
Just hope the OP isn't offering advanced tutorials in PHP
@MarkBaker :.. :)..haha,, no..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.